spring @import注解使用场景
生活随笔
收集整理的這篇文章主要介紹了
spring @import注解使用场景
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、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");}@Overridepublic boolean sendMsg(String msg) {System.out.println("MsgSendServiceImpl-->sendMsg msg:" + msg);return true;}@Overridepublic 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;@Overridepublic 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{@Overridepublic 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());} }總結
以上是生活随笔為你收集整理的spring @import注解使用场景的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring BeanPostProce
- 下一篇: spring @bean 自动创建容器对