一 @Import導入組件,id默認是組件的全類名
1 //類中組件統一設置。滿足當前條件,這個類中配置的所有bean注冊才能生效;
2 @Conditional({WindowsCondition.class})
3 @Configuration
4 @Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
5 //@Import導入組件,id默認是組件的全類名
6 public class MainConfig2 {
7
8 //默認是單實例的
9 /**
10 * ConfigurableBeanFactory#SCOPE_PROTOTYPE
11 * @see ConfigurableBeanFactory#SCOPE_SINGLETON
12 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request
13 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion
14 * @return\
15 * @Scope:調整作用域
16 * prototype:多實例的:ioc容器啟動並不會去調用方法創建對象放在容器中。
17 * 每次獲取的時候才會調用方法創建對象;
18 * singleton:單實例的(默認值):ioc容器啟動會調用方法創建對象放到ioc容器中。
19 * 以后每次獲取就是直接從容器(map.get())中拿,
20 * request:同一次請求創建一個實例
21 * session:同一個session創建一個實例
22 *
23 * 懶加載:
24 * 單實例bean:默認在容器啟動的時候創建對象;
25 * 懶加載:容器啟動不創建對象。第一次使用(獲取)Bean創建對象,並初始化;
26 *
27 */
28 // @Scope("prototype")
29 @Lazy
30 @Bean("person")
31 public Person person(){
32 System.out.println("給容器中添加Person....");
33 return new Person("張三", 25);
34 }
35
36 /**
37 * @Conditional({Condition}) : 按照一定的條件進行判斷,滿足條件給容器中注冊bean
38 *
39 * 如果系統是windows,給容器中注冊("bill")
40 * 如果是linux系統,給容器中注冊("linus")
41 */
42
43 @Bean("bill")
44 public Person person01(){
45 return new Person("Bill Gates",62);
46 }
47
48 @Conditional(LinuxCondition.class)
49 @Bean("linus")
50 public Person person02(){
51 return new Person("linus", 48);
52 }
53
54 /**
55 * 給容器中注冊組件;
56 * 1)、包掃描+組件標注注解(@Controller/@Service/@Repository/@Component)[自己寫的類]
57 * 2)、@Bean[導入的第三方包里面的組件]
58 * 3)、@Import[快速給容器中導入一個組件]
59 * 1)、@Import(要導入到容器中的組件);容器中就會自動注冊這個組件,id默認是全類名
60 * 2)、ImportSelector:返回需要導入的組件的全類名數組;
61 * 3)、ImportBeanDefinitionRegistrar:手動注冊bean到容器中
62 * 4)、使用Spring提供的 FactoryBean(工廠Bean);
63 * 1)、默認獲取到的是工廠bean調用getObject創建的對象
64 * 2)、要獲取工廠Bean本身,我們需要給id前面加一個&
65 * &colorFactoryBean
66 */
67 @Bean
68 public ColorFactoryBean colorFactoryBean(){
69 return new ColorFactoryBean();
70 }
二 實現Condition進行注入
1 Springboot有大量的@ConditionXXXX注解
2
3 public class LinuxCondition implements Condition {
4
5 /**
6 * ConditionContext:判斷條件能使用的上下文(環境)
7 * AnnotatedTypeMetadata:注釋信息
8 */
9 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
10 // TODO是否linux系統
11 //1、能獲取到ioc使用的beanfactory
12 ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
13 //2、獲取類加載器
14 ClassLoader classLoader = context.getClassLoader();
15 //3、獲取當前環境信息
16 Environment environment = context.getEnvironment();
17 //4、獲取到bean定義的注冊類
18 BeanDefinitionRegistry registry = context.getRegistry();
19
20 String property = environment.getProperty("os.name");
21
22 //可以判斷容器中的bean注冊情況,也可以給容器中注冊bean
23 boolean definition = registry.containsBeanDefinition("person");
24 if(property.contains("linux")){
25 return true;
26 }
27
28 return false;
29 }
30
31 }
三 實現ImportSelector
1 public class MyImportSelector implements ImportSelector {
2
3 //返回值,就是到導入到容器中的組件全類名
4 //AnnotationMetadata:當前標注@Import注解的類的所有注解信息
5 public String[] selectImports(AnnotationMetadata importingClassMetadata) {
6 // TODO Auto-generated method stub
7 //importingClassMetadata
8 //方法不要返回null值
9 return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
10 }
11
12 }
四 實現ImportBeanDefinitionRegistrar
1 public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
2
3 /**
4 * AnnotationMetadata:當前類的注解信息
5 * BeanDefinitionRegistry:BeanDefinition注冊類;
6 * 把所有需要添加到容器中的bean;調用
7 * BeanDefinitionRegistry.registerBeanDefinition手工注冊進來
8 */
9 @Override
10 public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
11
12 boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red");
13 boolean definition2 = registry.containsBeanDefinition("com.atguigu.bean.Blue");
14 if(definition && definition2){
15 //指定Bean定義信息;(Bean的類型,Bean。。。)
16 RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
17 //注冊一個Bean,指定bean名
18 registry.registerBeanDefinition("rainBow", beanDefinition);
19 }
20 }
21
22 }
五 實現 FactoryBean
1 / /創建一個Spring定義的FactoryBean
2 public class ColorFactoryBean implements FactoryBean<Color> {
3
4 //返回一個Color對象,這個對象會添加到容器中
5 @Override
6 public Color getObject() throws Exception {
7 // TODO Auto-generated method stub
8 System.out.println("ColorFactoryBean...getObject...");
9 return new Color();
10 }
11
12 @Override
13 public Class<?> getObjectType() {
14 // TODO Auto-generated method stub
15 return Color.class;
16 }
17
18 //是單例?
19 //true:這個bean是單實例,在容器中保存一份
20 //false:多實例,每次獲取都會創建一個新的bean;
21 @Override
22 public boolean isSingleton() {
23 // TODO Auto-generated method stub
24 return false;
25 }
26
27 }
28