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

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

  • 定义:具备相同领域的功能组件集合,组合所形成的一个独立的单元。
  • 举例:@EnableWebMvc(自动组装webMvc)、@EnableAutoConfiguration(激活Springboot自动装配)等
  • 实现:注解方式、编程方式
框架实现@Enable 注解模块激活模块
Spring Framework@EnableWebMvcWeb MVC 模块
@EnableTransactionManagement事务管理模块
@EnableCachingCaching 模块
@EnableMBeanExportJMX 模块
@EnableAsync异步处理模块
EnableWebFluxWeb Flux 模块
@EnableAspectJAutoProxyAspectJ 代理模块
Spring Boot@EnableAutoConfiguration自动装配模块
@EnableManagementContextActuator 管理模块
@EnableConfigurationProperties配置属性绑定模块
@EnableOAuth2SsoOAuth2 单点登录模块
Spring Cloud@EnableEurekaServerEureka服务器模块
@EnableConfigServer配置服务器模块
@EnableFeignClientsFeign客户端模块
@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)
可以看到 基于接口模块 编程方式 实现上知识 中间 多绕了一个接口实现方法 通过接口可以多一些弹性 灵活 可以多添加一些条件 来判断返回或者不返回多种返回值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值