目录
容器中注册BEAN的方式
- 包扫描+组件标注注解
@ComponentScan(basePackages = {"com.an.spring.condition"})
@Service
@Component
@Controller
@Repository - @BEan方式【导入第三方包里面的组件】
@ComponentScan(basePackages = {"com.an.spring.condition"}) public class ConditionConifg extends BaseConfig { @Bean @Conditional(WindowsCondition.class) public ListService wl() { return new WindowsListService(); } }
- @Import快速给容器中导入一个组件。
@Import({ColorBlue.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
1)、@IMport(要导入到容器中的组件),容器就会注入这个组件,默认BEAN在容器中的ID为类的全类名。@ComponentScan(basePackages = {"com.an.spring.condition"}) @Import(ColorBlue.class) public class ConditionConifg extends BaseConfig {} 效果: 当前容器中Bean: com.an.spring.other.ColorBlue
2)、在Import注解中类实现org.springframework.context.annotation.ImportSelector接口,返回需要导入的类全名数组。
public class MyImportSelector implements ImportSelector { /** * 返回值,就是导入到容器中的组件的全类名。 * * @param importingClassMetadata:当前标注@Import注解的类的所有注解信息 * @return 导入到容器中的组件的全类名【不可返回null】 */ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { System.out.println("MyImportSelector: " + importingClassMetadata.getClassName()); return new String[]{"com.an.spring.other.ColorRed"}; } }
3)、在Import注解中类实现org.springframework.context.annotation.ImportBeanDefinitionRegistrar接口,手动注册BEAN到容器中。
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * 注册BEAN * * @param importingClassMetadata 当前类的注解信息 * @param registry 通过调用BeanDefinitionRegistry#registerBeanDefinition(java.lang.String, * org.springframework.beans.factory.config.BeanDefinition)方法注册BEAN */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry); RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(ColorGreen.class); registry.registerBeanDefinition("rabinoooxx", beanDefinition); } }
- 使用Spring提供的org.springframework.beans.factory.FactoryBean(工厂BEAN)注册BEAN。
BEAN定义与配置:
FactoryBean配置:@ComponentScan(basePackages = {"com.an.spring.condition"}) @Import({ColorBlue.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) public class ConditionConifg extends BaseConfig { @Bean public MyFactoryBean myFactoryBean() { return new MyFactoryBean(); } }
package com.an.spring.condition; import com.an.spring.other.RainBow; import org.springframework.beans.factory.FactoryBean; public class MyFactoryBean implements FactoryBean<RainBow> { // 返回对象,这个会被添加到容器中 @Override public RainBow getObject() throws Exception { return new RainBow(); } @Override public Class<?> getObjectType() { return RainBow.class; } // 是否单例 @Override public boolean isSingleton() { return FactoryBean.super.isSingleton(); } }
获取FactoryBEAN的方式与特点:System.out.println("=====Spring提供的工厂BEAN对象获取:======"); System.out.println("获取工厂BEAN本身:" + context.getBean("&myFactoryBean")); System.out.println("获取工厂BEAN,getObject()方法返回的对象:" + context.getBean("myFactoryBean"));
BEAN生命周期