@configuration annotation class:
/ * * *@DescriptionConfiguration classes for testing@AuthorYounger brother younger brother *@CreateTime2019/6/18 * / desire
@Configuration
public class MyBeanConfig {
@Bean
public Country country(a){
return new Country();
}
@Bean
public UserInfo userInfo(a){
return newUserInfo(country()); }}Copy the code
The @Component annotation class:
/ * * *@DescriptionConfiguration classes for testing@AuthorYounger brother younger brother *@CreateTime 2019/6/18 14:36
*/
@Component
public class MyBeanConfig {
@Bean
public Country country(a){
return new Country();
}
@Bean
public UserInfo userInfo(a){
return newUserInfo(country()); }}Copy the code
Testing:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {
@Autowired
private Country country;
@Autowired
private UserInfo userInfo;
@Test
public void myTest() {
boolean result = userInfo.getCountry() == country;
System.out.println(result ? "One country" : "Different countries"); }}Copy the code
If @Configuration prints the same country, @Component prints different countries. Why?
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { @AliasFor( annotation = Component.class ) String value() default"";
}
Copy the code
You can click on @Configuration and see that it is also decorated by @Component, so context: Component-scan/or @ComponentScan can handle @Configuration classes.
Classes for the @Configuration tag must meet the following requirements:
Configuration classes must be provided as classes (not as instances returned by factory methods), allowing enhancement at run time by subclassing (Cglib dynamic proxies).
The configuration class cannot be final (there is no dynamic proxy).
Configuring annotations is typically done to generate Spring container-managed classes via @Bean annotations,
Configuration classes must be non-local (that is, cannot be declared in methods and cannot be private).
Any nested configuration class must be declared static.
The @bean method may not in turn create a further configuration class (that is, the returned Bean if it has
@configuration is not treated as a special bean, but as a normal bean.
However, the spring container has a special class for @Configuration at startup. It will enhance the @Configuration modified cglib dynamic proxy class. This is part of the reason why @Configuration needs to meet the above requirements. Here is a personal arrangement of ideas if there is a wrong please give directions
UserInfo () calls country(), and since it is a method, country() must generate a new contry(), so the dynamic proxy addition checks that if the method called in userInfo has @bean decorations, Instead of calling country(), which must bean object, beans in the spring container are singletons by default. Do not understand beans such as XML configuration
<bean id="country" class="com.hhh.demo.Country" scope="singleton"/>
Copy the code
Scope is a singleton by default.
The above is personal understanding, the analysis of the source see https://www.jb51.net/article/153430.htm for details
But if I just want to use @Component, what if a class without @Component has no dynamic proxy?
/ * * *@DescriptionConfiguration classes for testing@AuthorYounger brother younger brother *@CreateTime 2019/6/18 14:36
*/
@Component
public class MyBeanConfig {
@Autowired
private Country country;
@Bean
public Country country(a){
return new Country();
}
@Bean
public UserInfo userInfo(a){
return newUserInfo(country); }}Copy the code
So it’s guaranteed to be the same Country instance
If there is a mistake please big guys give directions thanks 0.0