场景:
应用:自己创建BertCondition、UserCondition的类实现Condition接口
public class BertDao {
public BertDao() {
System.out.println("创建构造器");
}
}
public class BertService {
public BertService() {
System.out.println("创建构造器BertService");
}
}
public class BertCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//判断容器中是否有bertDao的组件
if(context.getBeanFactory().containsBean("bertDao")) {
return true;
}
return false;
}
}
public class UserDao {
public UserDao() {
System.out.println("创建构造器UserDao");
}
}
public class UserService {
public UserService() {
System.out.println("创建构造器UserService");
}
}
public class UserCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//判断容器中是否有userDao的组件
if(context.getBeanFactory().containsBean("userDao")) {
return true;
}
return false;
}
}
public class ConditionalConfig {
@Bean
public BertDao bertDao() {
return new BertDao();
}
// 当容器中有bertDao的组件,那么bertService才会被实例化.
@Bean
@Conditional(value = BertCondition.class)
public BertService bertService() {
return new BertService();
}
// 当容器中有userDao的组件,那么userService才会被实例化
@Bean
@Conditional(value = UserCondition.class)
public UserService userService() {
return new UserService();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext(ConditionalConfig.class);
}
}
运行结果:
创建构造器BertDao
创建构造器BertService
1、通过@CompentScan +@Controller @Service @Respository @compent
适用场景: 针对我们自己写的组件可以通过该方式来进行加载到容器中。
2、通过@Bean的方式来导入组件(适用于导入第三方组件的类)
3、通过@Import来导入组件 (导入组件的id为全类名路径)
3.1、
public class BertImportBean {
public BertImportBean() {
System.out.println("创建构造器BertImportBean");
}
}
3.2、通过@Import 的ImportSeletor类实现组件的导入 (导入组件的id为全类名路径)
public class BertImportSelectBean {
public BertImportSelectBean() {
System.out.println("创建构造器BertImportSelectBean");
}
}
public class BertImportSelector implements ImportSelector {
// 可以获取导入类的注解信息
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.bert.importbean.BertImportSelectBean"};
}
}
3.3、通过@Import的 ImportBeanDefinitionRegister导入组件 (可以指定bean的名称)
public class BertImportDefinitionBean {
public BertImportDefinitionBean() {
System.out.println("创建构造器BertImportDefinitionBean");
}
}
public class BertBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//创建一个bean定义对象
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(BertImportDefinitionBean.class);
//把bean定义对象导入到容器中
registry.registerBeanDefinition("bertImportDefinitionBean", rootBeanDefinition);
}
}
3.4、测试
@Configuration
@Import(value = {BertImportBean.class, BertImportSelector.class, BertBeanDefinitionRegister.class})
public class ImportBeanConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportBeanConfig.class);
}
}
运行结果:
创建构造器BertImportBean
创建构造器BertImportSelectBean
创建构造器BertImportDefinitionBean
4、通过实现FacotryBean接口来实现注册 组件
public class BertFactory {
public BertFactory() {
System.out.println("创建构造器BertFactory");
}
}
public class BertFactoryBean implements FactoryBean {
// 返回bean的对象
@Override
public BertFactory getObject() throws Exception {
return new BertFactory();
}
// 返回bean的类型
@Override
public Class> getObjectType()
return BertFactory.class
}
// true: 单实例,每次获取都是同一个bean
// false : 多实例,每次获取都会创建一个新的bean
@Override
public boolean isSingleton() {
return false;
}
}
@Configuration
public class BertFactoryConfig {
@Bean
public BertFactoryBean bertFactoryBean() {
return new BertFactoryBean();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext(BertFactoryConfig.class);
System.out.println(ctx.getBean("bertFactoryBean"));
System.out.println(ctx.getBean("bertFactoryBean"));
}
}
运行结果:
创建构造器BertFactory
com.bert.factory.BertFactory@530612ba
创建构造器BertFactory
com.bert.factory.BertFactory@2a40cd94