我们知道springboot自动配置@EnableAutoConfiguration是通过@Import(AutoConfigurationImportSelector.class)来把自动配置组件加载进spring的context中的.

我们来看看@Import的定义:


/**
* Indicates one or more {@link Configuration @Configuration} classes to import.
*
* <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
* Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
* {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
* classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
*
* <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
* accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
* injection. Either the bean itself can be autowired, or the configuration class instance
* declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
* navigation between {@code @Configuration} class methods.
*
* <p>May be declared at the class level or as a meta-annotation.
*
* <p>If XML or other non-{@code @Configuration} bean definition resources need to be
* imported, use the {@link ImportResource @ImportResource} annotation instead.

 

翻译过来就是:

@Import用于声明引入一个或多个配置类,提供了等价于spring的XML配置中的<import/>元素的作用。

它允许引入带@Configuration的类,ImportSelector、ImportBeanDefinitionRegistrar的实现类,还有规则的组件类(AnnotationConfigApplicationContext类中通过register注册的注解,如@Componet、@Service等)。

在@Configuration的类中通过@Bean来声明的定义的类需要用@Autowired注入。不管是声明的配置类实例,还有配置类里面的bean,都能被自动注入。

后一种方法允许在@configuration类方法之间进行显式的、ide友好的导航。

@Import可以在类级别声明或作为元注释声明。

如果需要导入xml或其他非@configuration声明的bean定义,用importresource、importresource来代替之。

以下为自定义的自动配置代码:

1.声明一个自定义配置注解,@Import一个选择类。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(MyAutoConfigImportSelector.class)
public @interface MyEnableAutoConfig { }

2. 引入配置的类:

public class MyAutoConfigImportSelector implements ImportSelector {

    @Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"org.zhanglang.test.config.custemAutoConfig.TestBean"};
} }

3.定义一个bean

public class TestBean {

    private int id = 100;

    public int getId() {
return id;
} }

4.测试主方法

@MyEnableAutoConfig
public class MyApplication { public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyApplication.class);;
TestBean testBean = context.getBean(TestBean.class);
System.out.println(testBean.getId());//输出100
}
}

ok..测试程序控制台输出了100。说明注入TestBean成功。完成自定义配置引入bean。

最新文章

  1. Struts2日期类型转换
  2. 更新整理本人所有博文中提供的代码与工具(Java,2013.11)
  3. scala shuffle
  4. oracle 特殊符号
  5. android:#FFFFFFFF 颜色码解析
  6. 【Linux】linux常用基本命令
  7. iphone Dev 开发实例10:How To Add a Slide-out Sidebar Menu in Your Apps
  8. BZOJ2337: [HNOI2011]XOR和路径
  9. 【转】C++对象内存分配问题
  10. 《sort命令的k选项大讨论》-linux命令五分钟系列之二十七
  11. hdu2044java
  12. mysql优化, 删除数据后物理空间未释放(转载)
  13. 为Spring添加REST功能
  14. MVC模式学习笔记
  15. virtualenv 安装不同版本的虚拟环境的办法
  16. 五子棋AI大战OC实现
  17. Windows 服务多语言化时读取配置文件失败的问题。
  18. Java并发——线程池原理
  19. 基于RTMP的实时流媒体的QoE分析
  20. 取消导航栏navigationBar的半透明/毛玻璃效果

热门文章

  1. python numpy 的用法——diag函数
  2. nested exception is org.apache.ibatis.binding.BindingException
  3. vue封装swiper
  4. STM32WB SRAM2
  5. js excel导出功能
  6. 一键登录怎么在iOS端实现?这篇文章教会你!
  7. Codeforces Round #454 D. Power Tower (广义欧拉降幂)
  8. 51Nod 1534 棋盘阻挡博弈
  9. want
  10. 一道经典JS面试题