专栏名称: ImportNew
伯乐在线旗下账号,专注Java技术分享,包括Java基础技术、进阶技能、架构设计和Java技术领域动态等。
目录
相关文章推荐
ImportNew  ·  Java 之父怒斥:AI ... ·  2 天前  
芋道源码  ·  很严重了,大家极限搞钱吧 ·  17 小时前  
芋道源码  ·  30K ... ·  2 天前  
芋道源码  ·  很抱歉,考虑停更了,死磕AI暴利项目! ·  2 天前  
芋道源码  ·  Spring Boot + ... ·  2 天前  
51好读  ›  专栏  ›  ImportNew

SpringBoot 自动化配置的注解开关原理

ImportNew  · 公众号  · Java  · 2017-04-17 20:43

正文

请到「今天看啥」查看全文



在分析这些开关的原理之前,我们来看一个需求:


定义一个Annotation,让使用了这个Annotaion的应用程序自动化地注入一些类或者做一些底层的事情


我们会使用Spring提供的@Import注解配合一个配置类来完成。


我们以一个最简单的例子来完成这个需求:定义一个注解EnableContentService,使用了这个注解的程序会自动注入ContentService这个bean。


@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Import(ContentConfiguration.class)

public @interface EnableContentService {}

public interface ContentService {

void doSomething();

}

public class SimpleContentService implements ContentService {

@Override

public void doSomething() {

System.out.println("do some simple things");

}

}


然后在应用程序的入口加上@EnableContentService注解。


这样的话,ContentService就被注入进来了。 SpringBoot也就是用这个完成的。只不过它用了更加高级点的ImportSelector。


ImportSelector的使用


用了ImportSelector之后,我们可以在Annotation上添加一些属性,然后根据属性的不同加载不同的bean。


我们在@EnableContentService注解添加属性policy,同时Import一个Selector。


@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Import(ContentImportSelector.class)

public @interface EnableContentService {

String policy() default "simple";

}


这个ContentImportSelector根据EnableContentService注解里的policy加载不同的bean。


public class ContentImportSelector implements ImportSelector {

@Override

public String[] selectImports(AnnotationMetadata importingClassMetadata) {

Class> annotationType = EnableContentService.class;

AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(

annotationType.getName(), false));

String policy = attributes.getString("policy");







请到「今天看啥」查看全文