Spring is injected into beans into the IOC container

a @Import imports the component, the id is the full class name of the component by default

 1 //The components in the class are set uniformly. All bean registrations configured in this class will take effect when the current conditions are met;

2 @Conditional({WindowsCondition.class})
3 @Configuration
4 @Import({Color.class,Red .class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
5 //@Import imports components, id is the full class name of the component by default
6 public class MainConfig2 {
7
8 //The default is single instance
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 * @return15 * @Scope: adjust scope
16 * Prototype: multi-instance: ioc container will not start To call the method to create the object in the container.
17 * The method is called to create an object every time it is acquired;
18 * singleton: singleton (default): ioc container The startup will call the method to create the object and place it in the ioc container.
19 * Each subsequent acquisition is directly from the container (map.get ()) Take in,
20 * request: request to create an instance at the same time
21 * session: create an instance of the same session
22 *
23 * Lazy loading:
24 * Single-instance bean: By default, the object is created when the container starts ;
25 * Lazy loading: no object is created when the container starts. Use (obtain) Bean for the first time to create an object and initialize it;
26 *
27 */
28 // @Scope("prototype")
29 @Lazy
30 @Bean("person")
31 public Person person(){
32 System.out.println("Add Person....");
33 return new Person("Zhang San", 25);
34 }
35
36 /**
37 * @Conditional({Condition}): Determine according to certain conditions and meet Condition to register the bean in the container
38 *
39 * If the system is windows, register in the container ("bill ")
40 * If it is a linux system, register in the container ("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 * Register components in the container;
56 * 1), package scanning + component annotation annotations (@Controller /@Service/@Repository/@Component) [Classes written by myself]
57 * 2), @Bean[imported third-party package s component]
58 * 3), @Import[Quickly import one into the container Component]
59 * 1), @Import (to be imported into the container Component); this component will be automatically registered in the container, and the id is the full class name by default
60 * 2), ImportSelector: returns the full list of components that need to be imported Class name array;
61 * 3), ImportBeanDefinitionRegistrar: manually register the bean to the container
62 * 4) Use the FactoryBean provided by Spring ;
63 * 1) The factory bean calls getObject by default. Object created
64 * 2) To obtain the factory bean itself, we need to give Add an & before id
65 * &colorFactoryBean
66 */
67 @Bean
68 public ColorFactoryBean colorFactoryBean(){
69 return new ColorFactoryBean();
70 }

Two realization of Condition for injection

 1 Springboot has a lot of @ConditionXXXX annotations

2
3 public class LinuxCondition implements Condition {
4 ?
5 /**
6 * ConditionContext: the context (environment) that can be used to determine the condition
7 * AnnotatedTypeMetadata: Annotated information
8 */
9 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
10 // Is TODO a Linux system?
11 //1. Beanfactory used by ioc can be obtained
12 ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
13 //2, get the class loader
14 ClassLoader classLoader = context.getClassLoader();
15 //3. Get current environment information
16 Environment environment = context.getEnvironment();
17 //4. Get the registered class defined by the bean
18 BeanDefinitionRegistry registry = context.getRegistry();
19 ?
20 String property = environment.getProperty("os.name") ;
21 ?
22 //You can judge the bean registration in the container, and you can also register the bean in the container
23 boolean definition = registry.containsBeanDefinition("person");
24 if(property.contains("linux" )){
25 return true;
26 }
27 ?
28 return false;
29 }
30 ?
31 }

Three realization of ImportSelector

 1 public class MyImportSelector implements ImportSelector {

2 ?
3 //The return value is the full class name of the component imported into the container
4 //AnnotationMetadata: All annotation information of the class currently annotated with @Import
5 public String[] selectImports(AnnotationMetadata importingClassMetadata) {
6 // TODO Auto-generated method stub
7 //importingClassMetadata
8 //Methods don’t return null values
9 return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
10 }
11 ?
12 }

Four realization of ImportBeanDefinitionRegistrar

 1 public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

2 ?
3 /**
4 * AnnotationMetadata: Annotation information of the current class
5 * BeanDefinitionRegistry: BeanDefinition registration class;
6 * Put all the beans that need to be added to the container; call
7 * BeanDefinitionRegistry.registerBeanDefinition is manually registered
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 //Specify Bean definition information; (Bean type, Bean...)
16 RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
17 //Register a bean, specify the bean name
18 registry.registerBeanDefinition("rainBow", beanDefinition);
19 }
20 }
21 ?
22 }

Five realization of FactoryBean

 1 / /Create a Spring-defined FactoryBean

2 public class ColorFactoryBean implements FactoryBean {
3 ?
4 //return a Color object, which will be added to the container
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 //Is it a singleton?
19 //true: This bean is a single instance, save a copy in the container
20 //false: Multiple instances, a new bean will be created every time you get;
21 @Override
22 public boolean isSingleton() {
23 // TODO Auto-generated method stub
24 return false;
25 }
26 ?
27 }
28 ?

 1 //The components in the class are set uniformly. All bean registrations configured in this class will take effect when the current conditions are met;

2 @Conditional({WindowsCondition.class})
3 @Configuration
4 @Import({Color.class,Red .class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
5 //@Import imports components, id is the full class name of the component by default
6 public class MainConfig2 {
7
8 //The default is single instance
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 * @return15 * @Scope: adjust scope
16 * Prototype: multi-instance: ioc container will not start To call the method to create the object in the container.
17 * The method is called to create an object every time it is acquired;
18 * singleton: singleton (default): ioc container The startup will call the method to create the object and place it in the ioc container.
19 * Each subsequent acquisition is directly from the container (map.get ()) Take in,
20 * request: request to create an instance at the same time
21 * session: create an instance of the same session
22 *
23 * Lazy loading:
24 * Single-instance bean: By default, the object is created when the container starts ;
25 * Lazy loading: no object is created when the container starts. Use (obtain) Bean for the first time to create an object and initialize it;
26 *
27 */
28 // @Scope("prototype")
29 @Lazy
30 @Bean("person")
31 public Person person(){
32 System.out.println("Add Person....");
33 return new Person("Zhang San", 25);
34 }
35
36 /**
37 * @Conditional({Condition}): Determine according to certain conditions and meet Condition to register the bean in the container
38 *
39 * If the system is windows, register in the container ("bill ")
40 * If it is a linux system, register in the container ("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 }

 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 }

 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 }

 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 }

 1 / /创建一个Spring定义的FactoryBean

2 public class ColorFactoryBean implements FactoryBean {
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 ?

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4723 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.