正文
在分析这些开关的原理之前,我们来看一个需求:
定义一个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");