Spring @Enable模块装配
- 定义:具备相同领域功能组件集合,组合所形成一个独立的单元。【简化配置--一起配置】
- 举例:@EnableWebMvc【自动组装与webMvc相关的东西】、@EnableAutoConfiguration【激活自动装配】等
- 实现方式:注解方式、编程方式
@Enable注解模块举例
框架实现 | @Enable注解模块 | 激活模块 |
Spring Framework | @EnableWebMvc | WebMvc模块 |
@EnableTransactionManagement | 事务管理模块 | |
@Enablecaching | Caching模块 | |
@EnableMBeanExport | JMX模块 | |
@EnableAsync | 异步处理模块 | |
@EnableWebFlux | Web Flux模块 | |
@EnableAspectJAutoProxy | AspectJ代理模块 | |
Spring Boot | @EnableAutoConfiguration | 自动装配模块 |
@EnableManagementContext | Actuator管理模块 | |
@EnableConfigurationPropertis | 配置属性绑定模块 | |
@EnableOAuth2Sso | OAuth2单点登录模块 | |
Spring Cloud | @EnableEurekaServer | Eureka服务器模块 |
@EnableConfigServer | 配置服务器模块 | |
@EnableFeignClients | Feign客户端模块 | |
@EnableZuulProxy | 服务网关Zuul模块 | |
| @EnableCriecuitBreaker | 服务熔断器模块 |
实现方式
基于注解驱动方式
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
// 注册注解驱动
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
// 注解驱动
@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...
}
基于接口编程方式
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 注册驱动
@Import({CachingConfigurationSelector.class})
public @interface EnableCaching {
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY;
int order() default 2147483647;
}
// 实现了一个ImportSelector接口
public class CachingConfigurationSelector extends AdviceModeImportSelector<EnableCaching> {
...
/**
* Returns {@link ProxyCachingConfiguration} or {@code AspectJCachingConfiguration}
* for {@code PROXY} and {@code ASPECTJ} values of {@link EnableCaching#mode()},
* respectively. Potentially includes corresponding JCache configuration as well.
*/
@Override
public String[] selectImports(AdviceMode adviceMode) {
// 判断产生不同的注解类
switch (adviceMode) {
case PROXY:
return getProxyImports();
case ASPECTJ:
return getAspectJImports();
default:
return null;
}
}
}
自定义方式
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//HelloWorldImportSelector-> HelloWorldConfiguration->helloWorld
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}
/**
* HelloWorld {@link ImportSelector} 实现
*/
public class HelloWorldImportSelector implements ImportSelector{
@Override
public Predicate<String> getExclusionFilter() {
return null;
}
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 用helloWorld导入--变成了bean
return new String[]{HelloWorldConfiguration.class.getName()};
}
}
@Configuration
public class HelloWorldConfiguration {
@Bean
public String helloWorld(){ // 方法名即Bean名称
return "Hello, World 2018";
}
}
/**
* 2021-03-30 14:13:57.572 INFO 17620 --- [ main] c.S.d.b.EnableHelloWorldBootstrap : Starting EnableHelloWorldBootstrap using Java 1.8.0_261 on MicroWin10-1647 with PID 17620 (C:\Users\Administrator\Desktop\workspace\技术栈\dive-in-spring-boot\target\classes started by Administrator in C:\Users\Administrator\Desktop\workspace\技术栈\dive-in-spring-boot)
* 2021-03-30 14:13:57.574 INFO 17620 --- [ main] c.S.d.b.EnableHelloWorldBootstrap : No active profile set, falling back to default profiles: default
* 2021-03-30 14:13:57.814 INFO 17620 --- [ main] c.S.d.b.EnableHelloWorldBootstrap : Started EnableHelloWorldBootstrap in 0.565 seconds (JVM running for 0.932)
* helloWorld Bean : Hello, World 2018
*/
《慕课--小马哥Spring Boot2.0》