07-组件注册-@Import-使用ImportSelector

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

//自定义需要返回导入的数组
public class MyImportSeletor implements ImportSelector {
	
	//返回值就是导入到容器中组件的全类名
	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		// TODO Auto-generated method stub
		
		//不要返回null值,否则会报空指针异常
		return new String[]{"com.ceshi.bean.Black"};
	}

}
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.ceshi.bean.Color;
import com.ceshi.bean.MyImportSeletor;
import com.ceshi.bean.PersonBean;
import com.ceshi.demo.MyFilterType;

//配置类==配置文件(xml)
@Configuration   //告诉Spring这个一个配制类
/**
 * @import[快速给容器导入组件]
 * 1】、@Import({Color.class}) :导入容器中的组件,容器中默认就会自动注册这个组件,id默认为类的全类名
 * 2】、ImportSelector : 返回需要导入的组件的全类名数组
 */
@Import({Color.class,MyImportSeletor.class})
public class BeanConfig4 {
	
	
	@Bean("person")
	public PersonBean personBean2(){
		return new PersonBean("lishi","20");
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值