自定义AutoConfiguration的实现
2018-05-27 本文已影响0人
a乐乐_1234
个人认为springboot相比spring有如下特点
- 自带web容器,只要具备jre环境,一行命令就可以启动
- 开箱即用的自动化配置功能,用户无需担心相关依赖,配置相关Bean,大部分技术springboot自动为你搞定,你只需要在配置文件中配点参数即可
- 模块化扩展,在官方autoConfiguration中没找到你要用的框架,没关系,仿照着写一个就好了,写得好你也可以发到maven库供其他人使用,避免重复造轮子。
4.既然扩展和使用这么简便,自然在springcloud中可以大显身手了,目前springcloud中已经有很多插件了,他们构成了一个体系。而这些插件都是使用了springboot,具备开箱即用,插件化。
那如何定制自己的插件呢?我们可以仿照FreeMarkerAutoConfiguration来写
- 定义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();
}
}
}
- 定义Properties类
@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties{
.....
}
- 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"
},
....
]
}
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>