@ Configuration and @Component as configuration classes

Time:2019-8-12

@ Configuration annotated classes:

/**
 *@ Configuration classes for Description testing
 *@ Author's younger brother
 * @CreateTime 2019/6/18 14:35
 */
@Configuration
public class MyBeanConfig {
 @Bean
 public Country country(){
  return new Country();
 }
 @Bean
 public UserInfo userInfo(){
  return new UserInfo(country());
 }
}

@ Component annotated classes:

/**
 *@ Configuration classes for Description testing
 *@ Author's younger brother
 * @CreateTime 2019/6/18 14:36
 */
@Component
public class MyBeanConfig {
 @Bean
 public Country country(){
  return new Country();
 }
 @Bean
 public UserInfo userInfo(){
  return new UserInfo(country());
 }
}

Test:

@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?"Same country": "different country");
  }

}

If @Configuration prints out the same country and @Component prints out different countries, why?


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
  @AliasFor(
    annotation = Component.class
  )
  String value() default "";
}

If you click on @Configuration, you will find that it is also modified by @Component, so context: component-scan/or @ComponentScan can handle classes annotated by @Configuration.

@ Configuration tagged classes must meet the following requirements:

Configuration classes must be provided as classes (not instances returned by factory methods), allowing for runtime enhancements (cglib dynamic proxies) by generating subclasses.

Configuration classes cannot be final classes (there is no dynamic proxy).

Configuration annotations are usually used to generate Spring container-managed classes through @Bean annotations.

Configuration classes must be non-local (that is, they cannot be declared in methods, they cannot be private).

Any nested configuration class must be declared static.

@ The bean method may not, in turn, create further configuration classes (that is, returned beans with

@ Configuration, which is not handled specially, will only be used as a common bean.

But when the spring container starts up, it has a class that specializes in @Configuration, which enhances the dynamic proxy of @Configuration-modified class cglib, which is part of the reason why @Configuration needs to meet the above requirements, and what exactly will be enhanced?

Here’s the idea of personal sorting out. If there’s a mistake, please point it out.

Country () is called in userInfo (), because it is the method that necessarily generates new contry () by country (), so the dynamic proxy increase will judge it. If the method invoked in userInfo is also modified by @Bean, then country instance in spring container will be invoked directly instead of country (), which must be an object. Because beans in spring containers are singletons by default. Do not understand beans such as XML configuration


<bean scope="singleton"/>

Here scope defaults to singleton.

These are personal understandings. See https://www.jb51.net/article/153430.htm for details of source code analysis.

But if I want to use @Component, what if there is no @Component class and no dynamic proxy?

/**
 *@ Configuration classes for Description testing
 *@ Author's younger brother
 * @CreateTime 2019/6/18 14:36
 */
@Component
public class MyBeanConfig {
 @Autowired
 private Country country;
 @Bean
 public Country country(){
  return new Country();
 }
 @Bean
 public UserInfo userInfo(){
  return new UserInfo(country);
 }
}

This guarantees the same Country instance.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Manjaro uses SS method (requires nodejs environment)

Just installed manjaro, need to installshadowsocks-qt5+Proxy SwitchyOmega, but the latest Chrome has long blocked the installation of non Google store CRX channels. Here is my solution, which is troublesome but usable. If you are familiar with NPM commands in nodejs, you won’t find it troublesome, because nodejs is used for compilation. Preparation environment: A VPS […]