一、@Import
# 用法
1)@Import(User.class):如果导入的是配置类,将会按照配置类正常解析,如果是个普通类就会解析成bean
2)@Import(实现了ImportSelector接口的类.class):可以一次性注册多个bean,返回一个String[],每一个值就是类的完整类路劲
3)@Import(MyImportBeanDefinitionRegistrar.class):可以一次性注册多个bean,通过BeanDefinitionRegistry来动态注册BeanDefinition
4)@Import(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("ComponentA's NoArgsConstructor was invoked!");
return new ComponentA();
}
}
2.2.2、MySpringConfig
/**
* @Author : 一叶浮萍归大海
* @Date: 2023/11/27 15:45
* @Description:
*/
@Slf4j
public class MySpringConfig {
@Bean
public 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("ComponentB's NoArgsConstructor was invoked!");
return new ComponentB();
}
}
2.4.3、MySpringConfig
/**
* @Author : 一叶浮萍归大海
* @Date: 2023/11/27 15:45
* @Description:
*/
//@Configuration
@Slf4j
public class MySpringConfig {
@Bean
public ComponentA componentA() {
ComponentB componentB1 = componentB();
ComponentB componentB2 = componentB();
log.info("componentB1:{},componentB2:{},(componentB1 == componentB2) ? {}",componentB1,componentB2,(componentB1 == componentB2));
return new ComponentA();
}
@Bean
public 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方法的单例