1.beans
package com.atguigu.bean; public class Blue { public Blue(){ System.out.println("blue...constructor"); } public void init(){ System.out.println("blue...init..."); } public void detory(){ System.out.println("blue...detory..."); } }
package com.atguigu.bean; public class Yellow { }
package com.atguigu.bean; public class Color { private Car car; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Color [car=" + car + "]"; } }
2.引入@Import
package com.atguigu.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;import com.atguigu.bean.Blue; import com.atguigu.bean.Color;import com.atguigu.bean.Red;import com.atguigu.condition.MyImportSelector; //类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效; @Conditional({WindowsCondition.class}) @Configuration @Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class}) //@Import导入组件,id默认是组件的全类名 public class MainConfig2 { /** * 给容器中注册组件; * 1)、包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类] * 2)、@Bean[导入的第三方包里面的组件] * 3)、@Import[快速给容器中导入一个组件] 默认引入的bean的名称是类的全路径名 * 1)、@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名 * 2)、ImportSelector:返回需要导入的组件的全类名数组; * 3)、ImportBeanDefinitionRegistrar:手动注册bean到容器中,可以避免全路径名 */ }
3.自定义ImportSelector
package com.atguigu.condition; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; //自定义逻辑返回需要导入的组件 public class MyImportSelector implements ImportSelector { //返回值,就是到导入到容器中的组件全类名 //AnnotationMetadata:当前标注@Import注解的类的所有注解信息 @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // TODO Auto-generated method stub //importingClassMetadata //方法不要返回null值 return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"}; } }
4.自定义ImportBeanDefinitionRegistrar,手动指定bean的名称,避免全路径名
package com.atguigu.condition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; import com.atguigu.bean.RainBow; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * AnnotationMetadata:当前类的注解信息 * BeanDefinitionRegistry:BeanDefinition注册类; * 把所有需要添加到容器中的bean;调用 * BeanDefinitionRegistry.registerBeanDefinition手工注册进来 */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red"); boolean definition2 = registry.containsBeanDefinition("com.atguigu.bean.Blue"); if(definition && definition2){ //指定Bean定义信息;(Bean的类型,Bean。。。) RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class); //注册一个Bean,指定bean名 registry.registerBeanDefinition("rainBow", beanDefinition); } } }
最正点的详解: