美文网首页我爱编程
自定义AutoConfiguration的实现

自定义AutoConfiguration的实现

作者: a乐乐_1234 | 来源:发表于2018-05-27 10:12 被阅读0次

个人认为springboot相比spring有如下特点

  1. 自带web容器,只要具备jre环境,一行命令就可以启动
  2. 开箱即用的自动化配置功能,用户无需担心相关依赖,配置相关Bean,大部分技术springboot自动为你搞定,你只需要在配置文件中配点参数即可
  3. 模块化扩展,在官方autoConfiguration中没找到你要用的框架,没关系,仿照着写一个就好了,写得好你也可以发到maven库供其他人使用,避免重复造轮子。
    4.既然扩展和使用这么简便,自然在springcloud中可以大显身手了,目前springcloud中已经有很多插件了,他们构成了一个体系。而这些插件都是使用了springboot,具备开箱即用,插件化。

那如何定制自己的插件呢?我们可以仿照FreeMarkerAutoConfiguration来写

  1. 定义FreeMarkerAutoConfiguration
@Configuration
//必须存在指定类
@ConditionalOnClass({ freemarker.template.Configuration.class,FreeMarkerConfigurationFactory.class })
//在指定配置类后面配置
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
//启用指定的properties类
@EnableConfigurationProperties(FreeMarkerProperties.class)
public class FreeMarkerAutoConfiguration {
    private final ApplicationContext applicationContext;
    private final FreeMarkerProperties properties;
    //定义一个构造器,形参会自动注入进来
    public FreeMarkerAutoConfiguration(ApplicationContext applicationContext,FreeMarkerProperties properties) {
        this.applicationContext = applicationContext;
        this.properties = properties;
    }
  
    //实例化完后调用,一般用于初始化
    @PostConstruct
    public void checkTemplateLocationExists() {
    
    }
    //可以定义内部类
    protected static class FreeMarkerConfiguration {
        //这样也可以注入
        @Autowired
    protected FreeMarkerProperties properties;
        
        
    }
    //内部还可以继续定义配置类
    @Configuration
    @ConditionalOnClass({ Servlet.class, FreeMarkerConfigurer.class })
    //上下文必须是WebApplicationContext
    @ConditionalOnWebApplication
    public static class FreeMarkerWebConfiguration extends FreeMarkerConfiguration {
        @Bean
        //在BeanFactory中不存在指定类型Bean时,当前Bean才会生效
    @ConditionalOnMissingBean(FreeMarkerConfig.class)
     public FreeMarkerConfigurer freeMarkerConfigurer() {
              FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
          applyProperties(configurer);
          return configurer;
      }
          
          @Bean
          public freemarker.template.Configuration freeMarkerConfiguration(FreeMarkerConfig configurer){
              return configurer.getConfiguration();
          }

        @Bean
         //在BeanFactory中不存在指定名称Bean时,当前Bean才会生效
        @ConditionalOnMissingBean(name = "freeMarkerViewResolver")
        //检查指定name的值是否为true,如果未找到,就使用matchIfMissing的值
        @ConditionalOnProperty(name = "spring.freemarker.enabled", matchIfMissing = true)
        public FreeMarkerViewResolver freeMarkerViewResolver() {
            FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
            this.properties.applyToViewResolver(resolver);
            return resolver;
        }
        
    @Bean
    @ConditionalOnMissingBean
   //检查spring是否启用Spring resource handling chain,没有启用当前Bean也不会生效
    @ConditionalOnEnabledResourceChain
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
           return new ResourceUrlEncodingFilter();
    }
     }
}
  1. 定义Properties类
@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties{
    .....
}
  1. classpath路径下添加spring-configuration-metadata.json
{
  "groups": [
      {
      "name": "spring.freemarker",
      "type": "org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties",
      "sourceType": "org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties"
      },
    {
      "name": "spring.freemarker.cache",
      "type": "java.lang.Boolean",
      "description": "Enable template caching.",
      "sourceType": "org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties",
      "defaultValue": false
    },
    {
      "name": "spring.freemarker.charset",
      "type": "java.nio.charset.Charset",
      "description": "Template encoding.",
      "sourceType": "org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties",
      "defaultValue": "UTF-8"
    },
    ....
   ]
}
  1. 添加依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

相关文章

网友评论

    本文标题:自定义AutoConfiguration的实现

    本文链接:https://www.haomeiwen.com/subject/wduxjftx.html