最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring為IOC容器注入Bean的五種方式詳解

 更新時(shí)間:2019年10月29日 09:16:07   作者:天宇軒-王  
這篇文章主要介紹了Spring為IOC容器注入Bean的五種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring為IOC容器注入Bean的五種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一 @Import導(dǎo)入組件,id默認(rèn)是組件的全類名

//類中組件統(tǒng)一設(shè)置。滿足當(dāng)前條件,這個(gè)類中配置的所有bean注冊(cè)才能生效;
@Conditional({WindowsCondition.class})
@Configuration
@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//@Import導(dǎo)入組件,id默認(rèn)是組件的全類名
public class MainConfig2 {
  
  //默認(rèn)是單實(shí)例的
  /**
   * ConfigurableBeanFactory#SCOPE_PROTOTYPE  
   * @see ConfigurableBeanFactory#SCOPE_SINGLETON 
   * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request
   * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion
   * @return\
   * @Scope:調(diào)整作用域
   * prototype:多實(shí)例的:ioc容器啟動(dòng)并不會(huì)去調(diào)用方法創(chuàng)建對(duì)象放在容器中。
   *         每次獲取的時(shí)候才會(huì)調(diào)用方法創(chuàng)建對(duì)象;
   * singleton:單實(shí)例的(默認(rèn)值):ioc容器啟動(dòng)會(huì)調(diào)用方法創(chuàng)建對(duì)象放到ioc容器中。
   *     以后每次獲取就是直接從容器(map.get())中拿,
   * request:同一次請(qǐng)求創(chuàng)建一個(gè)實(shí)例
   * session:同一個(gè)session創(chuàng)建一個(gè)實(shí)例
   * 
   * 懶加載:
   *   單實(shí)例bean:默認(rèn)在容器啟動(dòng)的時(shí)候創(chuàng)建對(duì)象;
   *   懶加載:容器啟動(dòng)不創(chuàng)建對(duì)象。第一次使用(獲取)Bean創(chuàng)建對(duì)象,并初始化;
   * 
   */
// @Scope("prototype")
  @Lazy
  @Bean("person")
  public Person person(){
    System.out.println("給容器中添加Person....");
    return new Person("張三", 25);
  }
  
  /**
   * @Conditional({Condition}) : 按照一定的條件進(jìn)行判斷,滿足條件給容器中注冊(cè)bean
   * 
   * 如果系統(tǒng)是windows,給容器中注冊(cè)("bill")
   * 如果是linux系統(tǒng),給容器中注冊(cè)("linus")
   */
  
  @Bean("bill")
  public Person person01(){
    return new Person("Bill Gates",62);
  }
  
  @Conditional(LinuxCondition.class)
  @Bean("linus")
  public Person person02(){
    return new Person("linus", 48);
  }
  
  /**
   * 給容器中注冊(cè)組件;
   * 1)、包掃描+組件標(biāo)注注解(@Controller/@Service/@Repository/@Component)[自己寫的類]
   * 2)、@Bean[導(dǎo)入的第三方包里面的組件]
   * 3)、@Import[快速給容器中導(dǎo)入一個(gè)組件]
   *   1)、@Import(要導(dǎo)入到容器中的組件);容器中就會(huì)自動(dòng)注冊(cè)這個(gè)組件,id默認(rèn)是全類名
   *   2)、ImportSelector:返回需要導(dǎo)入的組件的全類名數(shù)組;
   *   3)、ImportBeanDefinitionRegistrar:手動(dòng)注冊(cè)bean到容器中
   * 4)、使用Spring提供的 FactoryBean(工廠Bean);
   *   1)、默認(rèn)獲取到的是工廠bean調(diào)用getObject創(chuàng)建的對(duì)象
   *   2)、要獲取工廠Bean本身,我們需要給id前面加一個(gè)&
   *     &colorFactoryBean
   */
  @Bean
  public ColorFactoryBean colorFactoryBean(){
    return new ColorFactoryBean();
  }

二 實(shí)現(xiàn)Condition進(jìn)行注入

Springboot有大量的@ConditionXXXX注解

public class LinuxCondition implements Condition {
​
  /**
   * ConditionContext:判斷條件能使用的上下文(環(huán)境)
   * AnnotatedTypeMetadata:注釋信息
   */
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    // TODO是否linux系統(tǒng)
    //1、能獲取到ioc使用的beanfactory
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    //2、獲取類加載器
    ClassLoader classLoader = context.getClassLoader();
    //3、獲取當(dāng)前環(huán)境信息
    Environment environment = context.getEnvironment();
    //4、獲取到bean定義的注冊(cè)類
    BeanDefinitionRegistry registry = context.getRegistry();
​
    String property = environment.getProperty("os.name");
​
    //可以判斷容器中的bean注冊(cè)情況,也可以給容器中注冊(cè)bean
    boolean definition = registry.containsBeanDefinition("person");
    if(property.contains("linux")){
      return true;
    }
    return false;
  }
}

三 實(shí)現(xiàn)ImportSelector

public class MyImportSelector implements ImportSelector {
​
  //返回值,就是到導(dǎo)入到容器中的組件全類名
  //AnnotationMetadata:當(dāng)前標(biāo)注@Import注解的類的所有注解信息
  public String[] selectImports(AnnotationMetadata importingClassMetadata) {
    // TODO Auto-generated method stub
    //importingClassMetadata
    //方法不要返回null值
    return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
  }
​
}

四 實(shí)現(xiàn)ImportBeanDefinitionRegistrar

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
​
  /**
   * AnnotationMetadata:當(dāng)前類的注解信息
   * BeanDefinitionRegistry:BeanDefinition注冊(cè)類;
   *   把所有需要添加到容器中的bean;調(diào)用
   *   BeanDefinitionRegistry.registerBeanDefinition手工注冊(cè)進(jìn)來
   */
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    
    boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red");
    boolean definition2 = registry.containsBeanDefinition("com.atguigu.bean.Blue");
    if(definition && definition2){
      //指定Bean定義信息;(Bean的類型,Bean。。。)
      RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
      //注冊(cè)一個(gè)Bean,指定bean名
      registry.registerBeanDefinition("rainBow", beanDefinition);
    }
  }
}

五 實(shí)現(xiàn) FactoryBean

/ /創(chuàng)建一個(gè)Spring定義的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
​
  //返回一個(gè)Color對(duì)象,這個(gè)對(duì)象會(huì)添加到容器中
  @Override
  public Color getObject() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("ColorFactoryBean...getObject...");
    return new Color();
  }
​
  @Override
  public Class<?> getObjectType() {
    // TODO Auto-generated method stub
    return Color.class;
  }
​
  //是單例?
  //true:這個(gè)bean是單實(shí)例,在容器中保存一份
  //false:多實(shí)例,每次獲取都會(huì)創(chuàng)建一個(gè)新的bean;
  @Override
  public boolean isSingleton() {
    // TODO Auto-generated method stub
    return false;
  }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

北票市| 苍溪县| 三河市| 年辖:市辖区| 宜兴市| 喜德县| 鹿泉市| 江都市| 崇信县| 财经| 喀喇沁旗| 襄樊市| 衡阳市| 阳江市| 淮北市| 松溪县| 阜宁县| 长春市| 贡山| 五原县| 山东| 特克斯县| 永宁县| 钦州市| 萍乡市| 宁城县| 姚安县| 郓城县| 峨山| 白玉县| 庆元县| 阿勒泰市| 北海市| 临西县| 桂平市| 化德县| 奉节县| 建平县| 临高县| 历史| 武宁县|