一、SpringBoot 的 @Import 用于将指定的类实例注入之Spring IOC Container中。 SpringBoot 提供了 三种使用 @Import 将 类实例注入至 Spring IOC Container中 的实例。
二.直接注入
直接引入要注入的类即可。
package com.tpw.newday.service.people;
import com.tpw.newday.bean.PeopleBean;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* <h3>newday</h3>
* <p></p>
*
* @author : lipengyao
* @date : 2021-06-25 14:08:52
**/
@Scope("singleton")
@Service(value = "MsgSendServiceImpl")
//@Import({PeopleServiceImpl.class}) //直接注入
//@Import({PeopleImportSeletor.class}) //实现 ImportSelector 注入
@Import({PeopleImportBeanDefineRegister.class}) //实现 ImportBeanDefinitionRegistrar 接口 注入
public class MsgSendServiceImpl implements MsgSendService {
// @Autowired
// private IPeopleService peopleService;
@Value("${people.beanfactory}")
private String peopleBeanFactory;
@Bean(name = {"MsgPeopleBean"},autowire = Autowire.NO)
public PeopleBean msgPeopleBean(){
System.out.println(" MsgSendServiceImpl -->msgPeopleBean peopleBeanFactory: " + peopleBeanFactory);
return new PeopleBean("msg People");
}
@Bean(name = {"people1"},autowire = Autowire.NO)
public PeopleBean peopleBean1(){
return new PeopleBean("zhangsan");
}
@Override
public boolean sendMsg(String msg) {
System.out.println("MsgSendServiceImpl-->sendMsg msg:" + msg);
return true;
}
@Override
public PeopleBean getMsgPeopleBean() {
return null;
}
}
三.实现 ImportSelector 注入
根据此接口的实现体动态选择注册哪些类到容器。
package com.tpw.newday.service.people;
import cn.hutool.core.util.StrUtil;
import com.tpw.newday.utils.PropertiesUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;
import java.util.Properties;
/**
* <h3>newday</h3>
* <p></p>
*
* @author : lipengyao
* @date : 2021-06-30 15:10:06
**/
@Component
public class PeopleImportSeletor implements ImportSelector{
@Value("${people.beanfactory}")
private String peopleBeanFactory;
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
Properties properties = PropertiesUtils.getProperties("application-dev.properties");
peopleBeanFactory = properties.getProperty("people.beanfactory");
System.out.println(" PeopleImportSeletor -->selectImports peopleBeanFactory: " + peopleBeanFactory);
if (StrUtil.equals(peopleBeanFactory,"PeopleSpringFactory" )){
return new String[]{PeopleSpringFactory.class.getName()};
}else{
return new String[]{PeopleBeanFactory.class.getName()};
}
}
}
四.实现 ImportBeanDefinitionRegistrar 接口 注入
可以手动将外部的任何类BEAN注册到容器工厂中。
package com.tpw.newday.service.people;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* <h3>newday</h3>
* <p></p>
*
* @author : lipengyao
* @date : 2021-07-01 10:42:13
**/
public class PeopleImportBeanDefineRegister implements ImportBeanDefinitionRegistrar{
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Class[] classes = {PeopleBeanFactory.class,PeopleSpringFactory.class};
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(classes[0]);
registry.registerBeanDefinition("PeopleBeanFactory", beanDefinitionBuilder.getBeanDefinition());
BeanDefinitionBuilder beanDefinitionBuilder1 = BeanDefinitionBuilder.rootBeanDefinition(classes[1]);
registry.registerBeanDefinition("PeopleSpringFactory", beanDefinitionBuilder1.getBeanDefinition());
}
}