@Configuration與@Component作爲配置類的區別詳解

這篇文章主要介紹了@Configuration與@Component作爲配置類的區別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

@Configuration註解的類:

/**
 * @Description 測試用的配置類
 * @Author 弟中弟
 * @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註解的類:

/**
 * @Description 測試用的配置類
 * @Author 弟中弟
 * @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());
 }
}

測試:

@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 ? "同一個country" : "不同的country");
  }

}

如果是@Configuration打印出來的則是同一個country,@Component則是不同的country,這是爲什麼呢?

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

你點開@Configuration會發現其實他也是被@Component修飾的,因此context:component-scan/ 或者 @ComponentScan都能處理@Configuration註解的類。

@Configuration標記的類必須符合下面的要求:

配置類必須以類的形式提供(不能是工廠方法返回的實例),允許通過生成子類在運行時增強(cglib 動態代理)。

配置類不能是 final 類(沒法動態代理)。

配置註解通常爲了通過 @Bean 註解生成 Spring 容器管理的類,

配置類必須是非本地的(即不能在方法中聲明,不能是 private)。

任何嵌套配置類都必須聲明爲static。

@Bean 方法可能不會反過來創建進一步的配置類(也就是返回的 bean 如果帶有

@Configuration,也不會被特殊處理,只會作爲普通的 bean)。

但是spring容器在啓動時有個專門處理@Configuration的類,會對@Configuration修飾的類cglib動態代理進行增強,這也是@Configuration爲什麼需要符合上面的要求中的部分原因,那具體會增強什麼呢?

這裏是個人整理的思路 如果有錯請指點

userInfo()中調用了country(),因爲是方法那必然country()生成新的new contry(),所以動態代理增加就會對其進行判斷如果userInfo中調用的方法還有@Bean修飾,那就會直接調用spring容器中的country實例,不再調用country(),那必然是一個對象了,因爲spring容器中的bean默認是單例。不理解比如xml配置的bean

<bean id="country" class="com.hhh.demo.Country" scope="singleton"/>

這裏scope默認是單例。

以上是個人理解,詳情源碼的分析請看https://www.jb51.net/article/153430.htm

但是如果我就想用@Component,那沒有@Component的類沒有動態代理咋辦呢?

/**
 * @Description 測試用的配置類
 * @Author 弟中弟
 * @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);
 }
}

這樣就保證是同一個Country實例了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章