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");
    }

    @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());
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值