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

Spring容器注冊組件實(shí)現(xiàn)過程解析

 更新時(shí)間:2020年03月24日 09:53:30   作者:天際星痕  
這篇文章主要介紹了Spring容器注冊組件實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、@Configuration&@Bean給容器中注冊組件

@Configuration及@Bean的使用參考如下代碼:

package com.atguigu.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
 
import com.atguigu.bean.Person;
 
//配置類==配置文件
@Configuration //告訴Spring這是一個(gè)配置類
 
@ComponentScans(
    value = {
        @ComponentScan(value="com.atguigu",includeFilters = {
/*           @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
            @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
            @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})
        },useDefaultFilters = false)  
    }
    )
//@ComponentScan value:指定要掃描的包
//excludeFilters = Filter[] :指定掃描的時(shí)候按照什么規(guī)則排除那些組件
//includeFilters = Filter[] :指定掃描的時(shí)候只需要包含哪些組件
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照給定的類型;
//FilterType.ASPECTJ:使用ASPECTJ表達(dá)式
//FilterType.REGEX:使用正則指定
//FilterType.CUSTOM:使用自定義規(guī)則
public class MainConfig {
   
  //給容器中注冊一個(gè)Bean;類型為返回值的類型,id默認(rèn)是用方法名作為id
  @Bean("person")
  public Person person01(){
    return new Person("lisi", 20);
  }
 
}

2、@ComponentScan-自動(dòng)掃描組件&指定掃描規(guī)則

@CompoentScan的使用,參考1中以上代碼即可。

3、自定義TypeFilter指定過濾規(guī)則

參考如下代碼:

package com.atguigu.config;
 
import java.io.IOException;
 
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
 
public class MyTypeFilter implements TypeFilter {
 
  /**
   * metadataReader:讀取到的當(dāng)前正在掃描的類的信息
   * metadataReaderFactory:可以獲取到其他任何類信息的
   */
  @Override
  public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
      throws IOException {
    // TODO Auto-generated method stub
    //獲取當(dāng)前類注解的信息
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
    //獲取當(dāng)前正在掃描的類的類信息
    ClassMetadata classMetadata = metadataReader.getClassMetadata();
    //獲取當(dāng)前類資源(類的路徑)
    Resource resource = metadataReader.getResource();
     
    String className = classMetadata.getClassName();
    System.out.println("--->"+className);
    if(className.contains("er")){
      return true;
    }
    return false;
  }
 
}

4、@Scope-設(shè)置組件作用域

參考如下代碼:

5、@Lazy懶加載

6、@Conditional-按照條件注冊bean

package com.atguigu.condition;
 
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
 
//判斷是否linux系統(tǒng)
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定義的注冊類
    BeanDefinitionRegistry registry = context.getRegistry();
     
    String property = environment.getProperty("os.name");
     
    //可以判斷容器中的bean注冊情況,也可以給容器中注冊bean
    boolean definition = registry.containsBeanDefinition("person");
    if(property.contains("linux")){
      return true;
    }
     
    return false;
  }
 
}

7、@Import-給容器中快速導(dǎo)入一個(gè)組件

源代碼如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
 
  /**
   * {@link Configuration @Configuration}, {@link ImportSelector},
   * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
   */
  Class<?>[] value();
 
}
package com.atguigu.config;
 
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
 
import com.atguigu.bean.Blue;
import com.atguigu.bean.Color;
import com.atguigu.bean.ColorFactoryBean;
import com.atguigu.bean.Person;
import com.atguigu.bean.Red;
import com.atguigu.condition.LinuxCondition;
import com.atguigu.condition.MyImportBeanDefinitionRegistrar;
import com.atguigu.condition.MyImportSelector;
import com.atguigu.condition.WindowsCondition;
import com.atguigu.test.IOCTest;
 
//類中組件統(tǒng)一設(shè)置。滿足當(dāng)前條件,這個(gè)類中配置的所有bean注冊才能生效;
@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)行判斷,滿足條件給容器中注冊bean
   *
   *              如果系統(tǒng)是windows,給容器中注冊("bill")
   *              如果是linux系統(tǒng),給容器中注冊("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);
  }
 
  /**
   * 給容器中注冊組件;
   * 1)、包掃描+組件標(biāo)注注解(@Controller/@Service/@Repository/@Component)[自己寫的類]
   * 2)、@Bean[導(dǎo)入的第三方包里面的組件] 3)、@Import[快速給容器中導(dǎo)入一個(gè)組件]
   * 1)、@Import(要導(dǎo)入到容器中的組件);容器中就會(huì)自動(dòng)注冊這個(gè)組件,id默認(rèn)是全類名
   * 2)、ImportSelector:返回需要導(dǎo)入的組件的全類名數(shù)組;
   * 3)、ImportBeanDefinitionRegistrar:手動(dòng)注冊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();
  }
 
}

8、@Import-使用ImportSelect

8.1 配置如下:

8.2對(duì)應(yīng)的類需實(shí)現(xiàn)ImportSelector接口

package com.atguigu.condition;
 
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
 
//自定義邏輯返回需要導(dǎo)入的組件
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"};
  }
 
}

9、@Import-使用ImportBeanDefinitionRegistrar

9.1配置如下:

9.2對(duì)應(yīng)的類需實(shí)現(xiàn)ImportBeanDefinitionRegistrar接口

package com.atguigu.condition;
 
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
 
import com.atguigu.bean.RainBow;
 
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
 
  /**
   * AnnotationMetadata:當(dāng)前類的注解信息
   * BeanDefinitionRegistry:BeanDefinition注冊類;
   *   把所有需要添加到容器中的bean;調(diào)用
   *   BeanDefinitionRegistry.registerBeanDefinition手工注冊進(jìn)來
   */
  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);
      //注冊一個(gè)Bean,指定bean名
      registry.registerBeanDefinition("rainBow", beanDefinition);
    }
  }
 
}

10、使用FactoryBean注冊組件

10.1 配置如下:

10.2對(duì)應(yīng)的類需實(shí)現(xiàn)org.springframework.beans.factory.FactoryBean<T>接口

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

11、給容器中注冊組件-總結(jié)

/**
* 給容器中注冊組件;
* 1)、包掃描+組件標(biāo)注注解(@Controller/@Service/@Repository/@Component)[自己寫的類]
* 2)、@Bean[導(dǎo)入的第三方包里面的組件] 3)、@Import[快速給容器中導(dǎo)入一個(gè)組件]
* 1)、@Import(要導(dǎo)入到容器中的組件);容器中就會(huì)自動(dòng)注冊這個(gè)組件,id默認(rèn)是全類名
* 2)、ImportSelector:返回需要導(dǎo)入的組件的全類名數(shù)組;
* 3)、ImportBeanDefinitionRegistrar:手動(dòng)注冊bean到容器中 4)、使用Spring提供的
* FactoryBean(工廠Bean); 1)、默認(rèn)獲取到的是工廠bean調(diào)用getObject創(chuàng)建的對(duì)象
* 2)、要獲取工廠Bean本身,我們需要給id前面加一個(gè)& &colorFactoryBean
*/

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

相關(guān)文章

  • Java中線程用法總結(jié)

    Java中線程用法總結(jié)

    這篇文章主要介紹了Java中線程用法,實(shí)例總結(jié)了java中線程的常見使用技巧,需要的朋友可以參考下
    2015-06-06
  • SpringBoot中使用@ControllerAdvice注解詳解

    SpringBoot中使用@ControllerAdvice注解詳解

    這篇文章主要介紹了SpringBoot中使用@ControllerAdvice注解詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個(gè)Controller增強(qiáng)器,可對(duì)controller中被 @RequestMapping注解的方法加一些邏輯處理,需要的朋友可以參考下
    2023-10-10
  • SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證

    SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證

    本文主要介紹了SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • json解析時(shí)遇到英文雙引號(hào)報(bào)錯(cuò)的解決方法

    json解析時(shí)遇到英文雙引號(hào)報(bào)錯(cuò)的解決方法

    下面小編就為大家分享一篇json解析時(shí)遇到英文雙引號(hào)報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯

    Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯

    這篇文章主要為大家介紹了Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 一文詳解如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語句

    一文詳解如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語句

    在MyBatis中,動(dòng)態(tài)SQL是一個(gè)強(qiáng)大的特性,允許我們在XML映射文件或注解中編寫條件語句,根據(jù)運(yùn)行時(shí)的參數(shù)來決定SQL的具體執(zhí)行內(nèi)容,這篇文章主要給大家介紹了關(guān)于如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語句的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • SpringBoot2.x過后static下的靜態(tài)資源無法訪問的問題

    SpringBoot2.x過后static下的靜態(tài)資源無法訪問的問題

    這篇文章主要介紹了SpringBoot2.x過后static下的靜態(tài)資源無法訪問的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 2021最新IDEA的各種快捷鍵匯總

    2021最新IDEA的各種快捷鍵匯總

    掌握idea的各種快捷鍵,可以幫助我們開發(fā)程序,今天小編給大家?guī)韼追N比較常用的idea快捷鍵及一些快捷鍵介紹,對(duì)idea快捷鍵相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-05-05
  • Java實(shí)現(xiàn)發(fā)送郵件功能時(shí)碰到的坑

    Java實(shí)現(xiàn)發(fā)送郵件功能時(shí)碰到的坑

    之前用163郵箱發(fā)郵件時(shí)明明是成功的,但是使用中國移動(dòng)自己的郵箱時(shí),無論如何在linux服務(wù)器中都發(fā)送不成功。下面小編給大家說下我是怎么解決的,一起看下吧
    2016-06-06
  • JAVA線程sleep()和wait()詳解及實(shí)例

    JAVA線程sleep()和wait()詳解及實(shí)例

    這篇文章主要介紹了JAVA線程sleep()和wait()詳解及實(shí)例的相關(guān)資料,探討一下sleep()和wait()方法的區(qū)別和實(shí)現(xiàn)機(jī)制,需要的朋友可以參考下
    2017-05-05

最新評(píng)論

康定县| 宜州市| 板桥市| 楚雄市| 松阳县| 吴忠市| 井陉县| 安新县| 安顺市| 遵义县| 平昌县| 曲靖市| 灵武市| 盐源县| 蕲春县| 夏邑县| 寿光市| 清新县| 景洪市| 区。| 松桃| 措美县| 壤塘县| 新田县| 东港市| 金阳县| 五河县| 定日县| 资溪县| 枣庄市| 吉林市| 增城市| 昌黎县| 明光市| 松潘县| 句容市| 丹棱县| 手机| 济宁市| 宜章县| 曲水县|