我们知道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. 配置lombok到eclipse上去

    使用maven导入lombok.jar包,可以帮助我们省略掉getter/setting方法. 1.pom.xml 添加依赖: <dependency> <groupId>or ...

  2. 点击登录页面成功后,后端返回数据需要保存,在另外一个页面,发送ajax请求的时候需要登录返回数据的其中的一部分当做参数然后拿到新的数据

    对于这个怎么操作首先我们要在登录的ajax请求中把后端的数据保存到sessionstorage中,代码如下 登录ajax $.ajax({ type:'post', url:xxxxxxxxx, da ...

  3. php操作 cookie

    1,设置cookie <?php setcookie('key'); setcookie('key1','value1'); setcookie(***); setcookie('key4', ...

  4. java9 新特征

    Java 平台级模块系统 java模块化解决的问题:减少Java应用和Java核心运行时环境的大小与复杂性 模块化的 JAR 文件都包含一个额外的模块描述器.在这个模块描述器中, 对其它模块的依赖是通 ...

  5. 解决GitHub下载资源慢的问题

    打开 C:\Windows\System32\drivers\etc\hosts 添加 # GitHub 解决下载速度慢的问题 192.30.253.113 github.com 151.101.18 ...

  6. kbmMW均衡负载与容灾(1)

    kbmMW为均衡负载与容灾提供了很好的机制,支持多种实现方式,现在看看最简单的一种,客户端控制的容灾和简单的负载均衡. 现在,我们将kbmMWServer部署到不同的服务器,或者在同一服务器部署多份实 ...

  7. 第十一章· MHA高可用及读写分离

    一.MHA简介 1.1.作者简介 松信嘉範: MySQL/Linux专家 2001年索尼公司入职 2001年开始使用oracle 2004年开始使用MySQL 2006年9月-2010年8月MySQL ...

  8. Oracle【子查询】

    Oracle子查询:当一个查询依赖于另外一个查询的结果的时候,就需要使用子查询.单行子查询 :筛选条件不明确,需要执行一次查询且查询结果只有一个字段且字段值只有一个.注意:where子句中允许出现查询 ...

  9. BLE 5协议栈-链路层

    文章转载自:http://www.sunyouqun.com/2017/04/page/3/ 链路层LL(Link Layer)是协议栈中最重要的一层. 链路层的核心是状态机,包含广播.扫描.发起和连 ...

  10. 身份证js正则

    /* 根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位 ...