Springboot中@Ehable**学习

news/2024/6/29 17:56:00

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

    Springboot版本是1.5.release.

    Springboot中使用EnableAutoConfiguration注解,如下所示:

    List-1

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 * @return the classes to exclude
	 */
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	String[] excludeName() default {};

}

    主要是那个EnableAutoConfigurationImportSelector,继承了接口ImportSelector,方法String[] selectImports(AnnotationMetadata var1)返回的类都会注入到Sping容器中。

    我们来实现一个自己的ImportSelector。

       

                  图1

    如图1所示,我们定义自己的类SelectorA、SelectorB、SelectorC,定义MyImportSelector如下所示:

    List-2

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

public class MyImportSelector implements ImportSelector {
    //会将这个方法返回的数组,放到容器中
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"com.mjduan.project.springbootlearn.selector.SelectorA",
                "com.mjduan.project.springbootlearn.selector.SelectorB"};
    }
}

    之后定义自己的EnableMyX,如下List-3

    List-3

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({MyImportSelector.class})
public @interface EnableMyX {
}

    在main方法的类上加上注解EnableMyX,如下所示:

    List-4

import com.mjduan.project.springbootlearn.enable.EnableEcho;
import com.mjduan.project.springbootlearn.selector.EnableMyX;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableMyX
public class SpringbootLearnApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootLearnApplication.class, args);
    }
}

    我们通过BeanPostProcessor来验证SelectorA和SelectorB加入到容器中了,如下

    List-5

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyImportSelectorBeanPostProcessor implements BeanPostProcessor {
    private String[] classess={
            "com.mjduan.project.springbootlearn.selector.SelectorA",
        "com.mjduan.project.springbootlearn.selector.SelectorB"};

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        for (String str : classess) {
            if (o.getClass().getName().equals(str)) {
                System.out.println(o.getClass().getName());
            }
        }
        return o;
    }
}

    之后启动应用,在控制台就可以看到打印出的SelectorA和SelectorB了。

    ImportSelector返回的String[]会被注入到Spring容器中,EnableAutoConfiguration中EnableAutoConfigurationImportSelector返回的是META-INF/spring.factories中org.springframework.boot.autoconfigure.EnableAutoConfiguration的值,这些值都是些类名,这些类上都有@Configuration注解,这样就将这个配置类中的bean加入到容器中。

思考

  1. 问题1,ImportSelector返回的String[]是怎么加入到Spring容器中的,在Springboot启动流程中的哪个步骤呢?

Reference

  1. https://www.jianshu.com/p/3da069bd865c

转载于:https://my.oschina.net/u/2518341/blog/3016359


http://www.niftyadmin.cn/n/2745330.html

相关文章

产妇坐月子可以碰水吗?

产妇坐月子可以碰水吗? 坐月子对于产妇来说是很重要的&#xff0c;这不仅仅是一项历史传统。若是在坐月子期间没有好好护理&#xff0c;很容易影响恢复&#xff0c;甚至可以出现各种后遗症。那么&#xff0c;产妇坐月子可以碰水吗?这个问题是很多朋友想要了解的。下面我们一起…

新版ButterKnifeの坑

2019独角兽企业重金招聘Python工程师标准>>> 日常习惯用ButterKnife进行控件绑定 GIitHub 心血来潮... 升级使用最新版本 10.1.0编译不能通过 dependencies {implementation com.jakewharton:butterknife:10.1.0annotationProcessor com.jakewharton:butterknife-co…

ArcGIS中的多个栅格波段合成一幅影像

此处用到了ArcGIS栅格处理中的Composite Bands工具&#xff08; Data Management Tools --> Raster --> Raster Processing&#xff09;。具体操作如下图所示。

产妇做月子期间有哪些注意事项?

产妇做月子期间有哪些注意事项? 坐月子女性朋友一生中很重要的时刻&#xff0c;这个时候可以改变他们的体质&#xff0c;所以在做月子的时候要注意保护&#xff0c;那么&#xff0c;产妇做月子期间有哪些注意事项?针对这个问题&#xff0c;下面就一起来看看专家的介绍。 产妇…

Django的restframework的序列化组件之对单条数据的处理

之前我们学习的都是处理书籍或者出版社的所有的数据的方法&#xff0c;下面我们来看下处理单个书籍&#xff0c;或者单个出版社的方法 这个时候我们就需要重新写一个类&#xff0c;这个类的方法&#xff0c;就需要有3个参数&#xff0c;参数1是self&#xff0c;参数2是request&…

MySQL SQL优化教程

转自:https://www.cnblogs.com/duanxz/archive/2013/02/01/2889413.html 一&#xff0c;查询SQL执行效率 通过show status命令了解各种SQL的执行效率。 ?123456789101112131415161718mysql> show status like Com_%;----------------------------------| Variable_name …

关于Flash的一些介绍(一)

一、flash介绍 Flash的前身是Future Wave公司的Future Splash&#xff0c;是世界上第一个商用的二维矢量动画软件&#xff0c;用于设计和编辑Flash文档。1996年美国Macromedia公司收购了Future Wave&#xff0c;并将其改名为Flash。在出到Flash 8以后&#xff0c;Macromedia又被…

P3311 [SDOI2014]数数

思路 看到多个子串并且不能包含的情况&#xff0c;想到了AC自动机 但是题目多了一个不能大于给出的n的限制条件&#xff0c;联想数位dp的过程&#xff0c;设f[i][j][0/1]表示在第i位&#xff0c;AC自动机的第j个节点&#xff0c;数位有/无限制的方案数 dp方程就是对应的转移到子…