1. @XxxxAuto
在SpringBoot中有很多以XxxxAutoConfiguration注解,其实他的作用就是,自动配置当前模块要依赖的类
例如:
@EnableAutoConfiguration
就告诉SpringBoot需要加载那些类,spring-boot-1.5.1.RELEASE.jar/META-INF/spring.factories 在该文件中
2. @Enable
@SpringBootApplication其实也是有以下三个注解组成的
@EnableAutoConfiguration 自动依赖当前所有模块的配置类org/springframework/boot/spring-boot-autoconfigure/1.5.2.RELEASE/spring-boot-autoconfigure-1.5.2.RELEASE.jar!/META-INF/spring.factories
@ComponentScan 扫描class
@Configuration 配置
当我们不在启动类添加@EnableAutoConfiguration时候,我们要自定义要依赖的模块,就要使用
@EnableAsync
@EnableScheduling
@EnableWebMVC
@EnableConfigurationProperties
@EnableJpaRepositories
@EnableTransactionManagement
@EnableCaching
其实@EnableAutoConfiguration这个注解,都是从自己的模块中查询spring.factories文件,
所以当应用启动就加载spring-boot-autoconfigure中的Spring.factories
代码中是
1
2
3
4
5
6
7
8
9
10类:AutoConfigurationImportSelector
方法:public String[] selectImports(AnnotationMetadata annotationMetadata)
AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader)
public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {
return loadMetadata(classLoader, "META-INF/spring-autoconfigure-metadata.properties");
}
具体的模块会导入不同的EnableConfigurationPropertiesImportSelector,然后复写selectImports方法,从当前类名中拿到包名+中依赖的信息,然后加载
1
2
3
4
5
6
7
8
9
10
11Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
Class extends Annotation> annotation() default Annotation.class;
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY;
int order() default Ordered.LOWEST_PRECEDENCE;
}
生注册Bean或者是添加配置时候我们可以更加细化@ConditionalOnClass : classpath中存在该类时起效
@ConditionalOnMissingClass : classpath中不存在该类时起效
@ConditionalOnBean : DI容器中存在该类型Bean时起效
@ConditionalOnMissingBean : DI容器中不存在该类型Bean时起效
@ConditionalOnSingleCandidate : DI容器中该类型Bean只有一个或@Primary的只有一个时起效
@ConditionalOnExpression : SpEL表达式结果为true时
@ConditionalOnProperty : 参数设置或者值一致时起效
@ConditionalOnResource : 指定的文件存在时起效
@ConditionalOnJndi : 指定的JNDI存在时起效
@ConditionalOnJava : 指定的Java版本存在时起效
@ConditionalOnWebApplication : Web应用环境下起效
@ConditionalOnNotWebApplication : 非Web应用环境下起效
执行顺序@AutoConfigureAfter:在指定的配置类初始化后再加载
@AutoConfigureBefore:在指定的配置类初始化前加载
@AutoConfigureOrder:数越小越先初始化
自定义Conditional约束类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public class ConditionalOtoSaasApplication extends SpringBootCondition{
@Override
public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Object name = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnMyProperties.class.getName()).get("name");
conditionContext.getEnvironment();
if (((String) name).equalsIgnoreCase("test")) {
return new ConditionOutcome(true, "get name properties");
}
return new ConditionOutcome(false, "no get name properties");
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ConditionalOtoSaasApplication.class)
public @interface ConditionalOnMyProperties {
String name();
}
@Configuration
@ConditionalOnMyProperties(name = "test")
public class BlmConfig{
private String url;
private String name;
}
↑
如果您觉得文章对你有用,可以赏我一杯咖啡
分享
新浪微博
微信
QQ空间
QQ好友
豆瓣
有道云笔记
取消