Spring Boot 笔记(二)@Enable 模块两种自定义实现

Spring Framework 3.1 开始支持”@Enable 模块驱动“。所谓“模块”是指具备相同领域的功能组件集合, 组合所形成一个独立 的单元。比如 Web MVC 模块、AspectJ代理模块、Caching(缓存)模块、JMX(Java 管 理扩展)模块、Async(异步处 理)模块等。
Spring @Enable 模块装配

  • 定义:具备相同领域的功能组件集合,组合所形成的一个独立的单元。
  • 举例:@EnableWebMvc(自动组装webMvc)、@EnableAutoConfiguration(激活Springboot自动装配)等
  • 实现:注解方式、编程方式
框架实现 @Enable 注解模块 激活模块
Spring Framework @EnableWebMvc Web MVC 模块
@EnableTransactionManagement 事务管理模块
@EnableCaching Caching 模块
@EnableMBeanExport JMX 模块
@EnableAsync 异步处理模块
EnableWebFlux Web Flux 模块
@EnableAspectJAutoProxy AspectJ 代理模块
Spring Boot @EnableAutoConfiguration 自动装配模块
@EnableManagementContext Actuator 管理模块
@EnableConfigurationProperties 配置属性绑定模块
@EnableOAuth2Sso OAuth2 单点登录模块
Spring Cloud @EnableEurekaServer Eureka服务器模块
@EnableConfigServer 配置服务器模块
@EnableFeignClients Feign客户端模块
@EnableZuulProxy 服务网关 Zuul 模块
@EnableCircuitBreaker 服务熔断模块

实现方式

注解驱动方式
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.TYPE)
@Documented 
@Import(DelegatingWebMvcConfiguration.class) 
public @interface EnableWebMvc {
}
@Configuration
public class DelegatingWebMvcConfiguration extends
WebMvcConfigurationSupport {
...
}
接口编程方式
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Import(CachingConfigurationSelector.class) 
 public @interface EnableCaching {
...
}
 public class CachingConfigurationSelector extends AdviceModeImportSelector<EnableCaching> {
    /**
    * {@inheritDoc}
    * @return {@link ProxyCachingConfiguration} or {@code
    AspectJCacheConfiguration} for
    * {@code PROXY} and {@code ASPECTJ} values of {@link
    EnableCaching#mode()}, respectively
    */
    public String[] selectImports(AdviceMode adviceMode) {
        switch (adviceMode) {
            case PROXY:
			return new String[] { AutoProxyRegistrar.class.getName(),ProxyCachingConfiguration.class.getName() };
        case ASPECTJ:
            return new String[] {
AnnotationConfigUtils.CACHE_ASPECT_CONFIGURATION_CLASS_NAME }; default:
} }

在这里插入图片描述
在这里插入图片描述

基于注解驱动实现

/**
 * Created by caomaoboy  2019-11-11
 **/
 //实体bean
@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String helloworld(){
        return "hell world 2018";
    }
}
/**
 * Created by caomaoboy 2019-11-11
 **/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}
/**
 * Created by caomaoboy 2019-11-11
 **/
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
    public static void main(String[] args) {
        ConfigurableApplicationContext content = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class).web(WebApplicationType.NONE).run(args);
        String hellworld  =content.getBean("helloworld",String.class);
        System.out.println("hello world"+hellworld);
        content.close();

    }
}

自定义接口组装bean

/**
 * Created by caomaoboy  2019-11-11
 **/
@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String helloworld(){
        return "hell world 2018";
    }
}
/**
 * Created by caomaoboy 2019-11-11
 **/
 //实现一个接口 来返回bean数组 有弹性 可以实现多种自定义返回值方式
public class HelloWorldImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{HelloWorldConfiguration.class.getName()};
    }
}
/**
 * Created by caomaoboy 2019-11-11
 **/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}

/**
 * Created by caomaoboy 2019-11-11
 **/
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
    public static void main(String[] args) {
        ConfigurableApplicationContext content = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class).web(WebApplicationType.NONE).run(args);
        String hellworld  =content.getBean("helloworld",String.class);
        System.out.println("hello world"+hellworld);
        content.close();

    }
}

注解实现:HelloWorldConfiguration -> HelloWorld(bean)
接口实现: HelloWorldImportSelector -> HelloWorldConfiguration -> HelloWorld(bean)
可以看到 基于接口模块 编程方式 实现上知识 中间 多绕了一个接口实现方法 通过接口可以多一些弹性 灵活 可以多添加一些条件 来判断返回或者不返回多种返回值。

发布了67 篇原创文章 · 获赞 5 · 访问量 3193

猜你喜欢

转载自blog.csdn.net/weixin_41315492/article/details/103009685