给容器中注册组件:
1.包扫描+组件标注注解(@Controller@Service@Repository@Component) [自己写的]
2.@Bean [导入的第三方包里边的组件]
3.@Import [快速给容器中导入一个组件]
1).@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
2).ImportSelector:返回需要导入的组件的全类名数组;
3).ImportBeanDefinitionRegistrar:手动注册bean到容器中
4.使用Spring提供的 FactoryBean(工厂Bean);
1).默认获取到的是工厂bean调用getObject创建的对象
2).要获取工厂Bean本身,我们需要给id前面加一个&,例如:&colorFactoryBean
1.2.见 组件注册(一)–@Configuration&@Bean给容器中注册组件
https://blog.csdn.net/qq_39548700/article/details/105995684
3.1代码
准备两个类Color.class、Red.class(里边可以什么都不写)
配置文件MainConfig2.java中增加@Import注解
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.propertyeditors.ReaderEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.atguigu.bean.Color;
import com.atguigu.bean.Person;
import com.atguigu.bean.Red;
import com.atguigu.condition.LinuxCondition;
import com.atguigu.condition.WindowsCondition;
@Conditional({WindowsCondition.class})
@Configuration
@Import({Color.class,Red.class})
//导入组件:id默认是组件的全类名
public class MainConfig2 {
@Lazy
@Bean("person")
public Person person() {
System.out.println("给容器中添加Person....");
return new Person("张三",25);
}
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
}
测试类IOCTest.java
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.atguigu.config.MainConfig2;
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void testImport(){
printBeans(applicationContext);
}
private void printBeans(AnnotationConfigApplicationContext applicationContext) {
String[] definitonNames = applicationContext.getBeanDefinitionNames();
for(String name: definitonNames) {
System.out.println(name);
}
}
}
运行结果中除了用@Conditional按照条件注册bean之外,还有用Import注册进入的全类名组件
3.2代码
创建两个导入容器的组件类Blue.java、Yellow.java(里边可以什么都不写)
需要一个类implements ImportSelector,重写方法符合条件的组件导入到容器中
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
//自定义逻辑返回需要导入的组件
public class MyImportSelector implements ImportSelector {
//返回值,就是到导入到容器中的组件全类名
//AnnotationMetadata:当前标注@Import注解的类的所有注解信息
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//方法不要返回null值
return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
}
}
配置文件MainConfig.java中@Import注解中增加MyImportSelector.class
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.propertyeditors.ReaderEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.atguigu.bean.Color;
import com.atguigu.bean.Person;
import com.atguigu.bean.Red;
import com.atguigu.condition.LinuxCondition;
import com.atguigu.condition.WindowsCondition;
@Conditional({WindowsCondition.class})
@Configuration
@Import({Color.class,Red.class,MyImportSelector.class})
//导入组件:id默认是组件的全类名
public class MainConfig2 {
@Lazy
@Bean("person")
public Person person() {
System.out.println("给容器中添加Person....");
return new Person("张三",25);
}
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
测试类IOCTest.java
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.atguigu.config.MainConfig2;
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void testImport(){
printBeans(applicationContext);
Blue bean = applicationContext.getBean(Blue.class);
System.out.println(bean);
}
private void printBeans(AnnotationConfigApplicationContext applicationContext) {
String[] definitonNames = applicationContext.getBeanDefinitionNames();
for(String name: definitonNames) {
System.out.println(name);
}
}
}
运行结果
3.3代码
创建两个导入容器的组件类RainRow.java(里边可以什么都不写)
需要一个类implements ImportBeanDefinitionRegistrar,重写方法自定义组件导入到容器中
我这里的条件是但容器中存在com.atguigu.bean.Red、com.atguigu.bean.Blue组件,注册RainRow组件
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.registerBeanDifinition手工注册进来
*/
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("rainRow", beanDefinition);
}
}
}
配置文件MainConfig.java中@Import注解中增加MyImportBeanDefinitionRegistrar.class
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.propertyeditors.ReaderEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.atguigu.bean.Color;
import com.atguigu.bean.Person;
import com.atguigu.bean.Red;
import com.atguigu.condition.LinuxCondition;
import com.atguigu.condition.WindowsCondition;
@Conditional({WindowsCondition.class})
@Configuration
@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//导入组件:id默认是组件的全类名
public class MainConfig2 {
@Lazy
@Bean("person")
public Person person() {
System.out.println("给容器中添加Person....");
return new Person("张三",25);
}
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
测试类IOCTest.java
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.atguigu.config.MainConfig2;
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void testImport(){
printBeans(applicationContext);
Blue bean = applicationContext.getBean(Blue.class);
System.out.println(bean);
}
private void printBeans(AnnotationConfigApplicationContext applicationContext) {
String[] definitonNames = applicationContext.getBeanDefinitionNames();
for(String name: definitonNames) {
System.out.println(name);
}
}
}
运行结果