郑州网站建设品牌,郑州app开发哪家好,中国外协加工订单网,和县网站建设一、Import # 用法 1#xff09;Import(User.class)#xff1a;如果导入的是配置类#xff0c;将会按照配置类正常解析#xff0c;如果是个普通类就会解析成bean 2#xff09;Import#xff08;实现了ImportSelector接口的类.class#xff09;#xff1a;可以一次性注册…一、Import # 用法 1Import(User.class)如果导入的是配置类将会按照配置类正常解析如果是个普通类就会解析成bean 2Import实现了ImportSelector接口的类.class可以一次性注册多个bean返回一个String[]每一个值就是类的完整类路劲 3Import(MyImportBeanDefinitionRegistrar.class)可以一次性注册多个bean通过BeanDefinitionRegistry来动态注册BeanDefinition 4Import(MyDeferredImportSelector.class) 二、Configuration
2.1、概述 Configuration是用来代替传统的xml的配置方式配置bean的。 2.2、不加就不能配置bean吗 答能。 2.2.1、ComponentA
/*** Author : 一叶浮萍归大海* Date: 2023/11/27 15:45* Description:*/
public class ComponentA {public ComponentA componentA() {System.out.println(ComponentAs NoArgsConstructor was invoked!);return new ComponentA();}}
2.2.2、MySpringConfig
/*** Author : 一叶浮萍归大海* Date: 2023/11/27 15:45* Description:*/
Slf4j
public class MySpringConfig {Beanpublic ComponentA componentA() {return new ComponentA();}}
2.2.3、SpringConfigurationMainApp
/*** Author : 一叶浮萍归大海* Date: 2023/11/27 15:46* Description:*/
Slf4j
public class SpringConfigurationMainApp {public static void main(String[] args) {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(MySpringConfig.class);ComponentA componentA context.getBean(componentA, ComponentA.class);log.info(componentA:{},componentA);}} 2.3、加与不加的区别 配置类加了Configuration注解Spring会为配置类创建cglib动态代理Bean方法的调用就会通过容器getBean进行获取保证当Bean方法进行互调时Bean是单例的。 2.4、不加Configuration注解案例
2.4.1、ComponentA同2.2.1
2.4.2、ComponentB
/*** Author : 一叶浮萍归大海* Date: 2023/11/27 15:45* Description:*/
public class ComponentB {public ComponentB componentB() {System.out.println(ComponentBs NoArgsConstructor was invoked!);return new ComponentB();}}
2.4.3、MySpringConfig
/*** Author : 一叶浮萍归大海* Date: 2023/11/27 15:45* Description:*/
//Configuration
Slf4j
public class MySpringConfig {Beanpublic ComponentA componentA() {ComponentB componentB1 componentB();ComponentB componentB2 componentB();log.info(componentB1:{},componentB2:{},(componentB1 componentB2) ? {},componentB1,componentB2,(componentB1 componentB2));return new ComponentA();}Beanpublic ComponentB componentB() {return new ComponentB();}}
2.4.4、SpringConfigurationMainApp同上 2.5、加Configuration注解案例 代码同2.4但是要在MySpringConfig上标注Configuration注解。 2.6、结论 虽然配置类上加与不加Configuration注解都可以创建bean但是当Bean方法内部调用另一个Bean标注的方法时如果配置类没有加Configuration注解那么Spring将不会为其创建Cglib动态代理而是将被调用的Bean方法当做一个普通方法于是就出现了2.4中的结果。 2.7、原理 1、启动IOC容器的时候会注册一个解析配置的处理器 ConfigurationClassPostProcessor 2、调用invokeBeanFactoryPostProcessors(beanFactory)时就会去调用postProcessBeanDefinitionRegistry进行解析配置解析加了Configuration、Component、Import、Bean的类目的是为了注册BeanDefinition 3、ConfigurationClassPostProcessor.postProcessBeanFactory去创建cglib动态代理 4、当Bean方法进行互调时会通过cglib进行增强通过调用的方法名作为bean的名称去IOC容器中获取进而保证了Bean方法的单例 三、SpringBootApplication
3.1、概述 SpringBootApplication是SpringBoot中的注解通常标识在主启动类上表明当前应用是一个SpringBoot工程它实际上是一个复合注解结构如下 四、SpringBootConfiguration
4.1、概述 SpringBootConfiguration也是SpringBoot中的注解实际上就是一个Configuration表示启动类也是一个配置类结构如下 五、EnableAutoConfiguration
5.1、概述 EnableAutoConfiguration也是SpringBoot中的注解它导入了一个AutoConfigurationImportSelector用于加载classpath/META-INF/spring.factories中所定义的所有自动配置类并将这些自定配置类注册为bean结构如下 六、Conditional
6.1、概述 Conditional注解是Spring中的一个注解主要用于xxxAutoConfiguration类中用于程序员自定义starter时进行扩展这些自动配置类在IOC容器加载过程中最后才被加载其中起到关键作用的注解就是ConditionalXxx例如ConditionalOnMissingClass({org.aspectj.weaver.Advice})、ConditionalOnClass({Advice.class})、ConditionalOnProperty(...等等结构如下