我们知道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。

spring自定义自动配置注解的更多相关文章

  1. 自定义的Spring Boot starter如何设置自动配置注解

    本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...

  2. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

  3. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  4. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  5. Springboot 系列(三)Spring Boot 自动配置原理

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...

  6. Spring Boot自动配置与Spring 条件化配置

    SpringBoot自动配置 SpringBoot的自动配置是一个运行时(应用程序启动时)的过程,简化开发时间,无需浪费时间讨论具体的Spring配置,只需考虑如何利用SpringBoot的自动配置即 ...

  7. Spring Boot自动配置原理与实践(一)

    前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...

  8. Spring Boot自动配置原理(转)

    第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...

  9. 使用@AutoConfigureBefore、After、Order调整Spring Boot自动配置顺序

    前言 Spring Boot是Spring家族具有划时代意义的一款产品,它发展自Spring Framework却又高于它,这种高于主要表现在其最重要的三大特性,而相较于这三大特性中更为重要的便是Sp ...

随机推荐

  1. js判断浏览器内核如果是ie弹出提示非ie不进行任何操作

    如上做一个弹出框针对ie兼容 // 获取IE版本 function IEVersion() { // 取得浏览器的userAgent字符串 var userAgent = navigator.user ...

  2. cookie、sessionStorage和localStorage区别

    // 数据存储 cookie:生命周期一般是手动设置失效的时间,大小为4k,易用性不高,需要自己封装(封装请看上一篇博客) sessionStorage:生命周期是浏览器关闭接失效,大小为5m或者更大 ...

  3. ln建立时符号链接时出现同名文件或目录

    给ln命令加上-s选项,则建立软链接. 格式:ln -s [真正的文件或者目录] [链接名]   [链接名]可以是任何一个文件名或者目录名,并且允许它与原文件不在同一个文件系统中. 如果[链接名]是一 ...

  4. Java的概述

    Java的基本概述 Java是SUN(Stanford University Network),斯坦福大学网络公司)1995年推出的一门高级编程语言.Java是一种面向Internet的编程语言.随着 ...

  5. bootstap 表格自动换行 截取超长数据

    <table class="table" style="TABLE-LAYOUT:fixed;WORD-WRAP:break_word">

  6. QTP(4)

    一.常见回放错误 1.The "XXX" XXX object was not found in the Object Repository.(在对象库中未找到对象) ...... ...

  7. Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)

    Diagonal Walking v.2 CodeForces - 1036B Mikhail walks on a Cartesian plane. He starts at the point ( ...

  8. eclipse 中离线安装activiti插件,报错“An error occurred while collecting items to be installed session context was:(...”

    eclipse 中离线安装activiti插件,报错“An error occurred while collecting items to be installed session context ...

  9. 网络协议相关面试问题-TLS与SSL握手

    HTTPS是什么? HTTPS并不是一个单独的协议,而是对工作在一加密连接(SSL / TLS)上的常规HTTP协议.通过在TCP和HTTP之间加入TLS(Transport Layer Securi ...

  10. Linux之vim文本编译器

    Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和Emacs并列成为类Unix系统用户最喜欢的编辑器. [root@izwz98re ...