一、组件注册
1、@Scope设置组件作用域
- prototype:多例,ioc容器启动并不会去调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象。
- singleton:单例(默认值),ioc容器启动会调用方法创建对象方到容器中。每次获取就是直接从容器中拿。
- request:同一次请求创建一个实例
- session:同一个session创建一个实例
@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Bean
@Scope(value = "prototype")
public Person person(){
return new Person(1,"张三");
}
}
2、@Lazy懒加载
- 单例bean:默认ioc容器启动的时候创建对象
- 懒加载bean:ioc容器启动不创建对象。第一次使用(获取)bean时候创建对象,只创建一次
@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Bean
@Lazy
public Person person(){
return new Person(1,"张三");
}
}
3、@Conditional按照条件加载bean
- 注释在方法:windows系统加载李雷,linux系统加载韩梅梅
- 注释在类上:满足条件整个类才会配置加载bean
@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Conditional({WindowsCondition.class})
@Bean("lilei")
public Person person01(){
return new Person(10,"李雷");
}
@Conditional({LinuxCondition.class})
@Bean("hanmeimei")
public Person person02(){
return new Person(20,"韩梅梅");
}
}
加载条件
public class LinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
ClassLoader classLoader = context.getClassLoader();
Environment environment = context.getEnvironment();
BeanDefinitionRegistry registry = context.getRegistry();
String property = environment.getProperty("os.name");
return property.contains("linux") ? true : false;
}
}
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String property = context.getEnvironment().getProperty("os.name");
return property.contains("Windows") ? true : false;
}
}
4、@Import为容器添加组件
- 传统方式:包扫描+组件标注注解(@Controller等)
- @Bean:导入第三方包里面的组件
1)@Import
容器中自动注册组件,id默认全类名
@Configuration
@ComponentScan("spring.module.annotation")
@Import({Red.class, Blue.class})
public class MainConfig {
}
打印ioc容器中所有组件
public class Client {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}
结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Red
spring.module.annotation.entity.Blue
2)@ImprotSelector
返回多个组件的全类名数组
@Configuration
@ComponentScan("spring.module.annotation")
@Import({MyImportSelector.class})
public class MainConfig {
}
自定义逻辑返回需要导入的组件
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"spring.module.annotation.entity.Black", "spring.module.annotation.entity.Yellow"};
}
}
三个注解信息分别为:@Configuration、@ComponentScan、@Import
结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Black
spring.module.annotation.entity.Yellow
3)@ImprotBeanDefinitionRegistrar
手动注册bean到容器中
@Configuration
@ComponentScan("spring.module.annotation")
@Import({MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
}
自定义手工注册
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean exist = registry.containsBeanDefinition("spring.module.annotation.entity.Black");
if(exist){
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(White.class);
registry.registerBeanDefinition("white",rootBeanDefinition);
}
}
}
结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Black
spring.module.annotation.entity.Yellow
white
|