模仿HttpEncodingConfiguration创建自定义自动配置,application.properties中的信息自动加载
Spring Boot封装好的自动配置很简单,在用得很轻松的同时,有没有想过,我们自己的项目中也根据自己的需求写一个具备Spring Boot自动配置功能的冲动呢?首先,来说说Http编码配置的例子,然后参考这个例子,我们自己来实现一个自动配置的功能。
Http编码配置
添加配置,即在 application.properties 上添加:
#HTTP encoding (HttpEncodingProperties) spring.http.encoding.charset=UTF-8 # the encoding of HTTP request/response spring.http.encoding.enabled=true #enabled http encoding support spring.http.encoding.force=true #force the configured encoding
当然,不一定要”application.properties”这样的文件名,我们可以新建一个”application-myauto.properties”文件,只需要在 @ConfigurationProperties 的属性 locations 里指定properties的位置即可,且需要在入口类上配置。下面举个例子:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.http.encoding",locations = {"classpath:config/application-myauto.properties"}) //1
public class MyAutoSettings {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
Spring Boot实现HttpEncoding自动配置的源码如下:
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.http.encoding")//在 application.properties 配置的时候前缀是 spring.http.encoding public class HttpEncodingProperties { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");//默认编码方式为UTF-8,若修改可使用 spring.http.encoding.charset=编码 private Charset charset = DEFAULT_CHARSET; private boolean force = true;//设置 forceEncoding,默认为true,若修改可使用 spring.http.encoding.force=false public Charset getCharset() { return this.charset; } public void setCharset(Charset charset) { this.charset = charset; } .... }
配置Bean,根据条件配置CharsetEncodingFilter的Bean
@Configuration @EnableConfigurationProperties(HttpEncodingProperties.class)//开启属性注入,通过@EnableConfigurationProperties 声明,使用 @Autowired注入 @ConditionalOnClass(CharacterEncodingFilter.class)//当CharacterEncodingFilter在类路径的条件下 @ConditionalOnProperties(prefix = "spring.http.encoding",value = "enabled",matchIfMissing = true)//当设置 spring.http.encoding = enabled 的情况下,如果没有设置则默认为true,即条件符合; public class HttpEncodingAutoConfiguration { @Autowired private HttpEncodingProperties httpEncodingProperties; @Bean // 像使用Java 配置的方式配置CharacterEncodingFilter 这个Bean @ConditionalOnMissingBean(CharacterEncodingFilter.class)//当容器中没有这个Bean的时候新建Bean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.httpEncodingProperties.getCharset().name()); filter.setForceEncoding(this.httpEncodingProperties.isForce()); return filter; } }
MyAutoConfig配置
下面就来创建一个我们自己项目的自动配置。
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.3.5.RELEASE</version> </dependency>
属性配置
1.application.properties中填入:
app.name=jmust app.source=wx
2.配置类
@ConfigurationProperties(prefix="app") public class MyAutoPrpperties { private static final String NAME = "jmust"; private String name = NAME; public String getName() { return name; } public void setName(String name) { this.name = name; } }
在 application.properties 中通过 app.name= 来设置,默认为 app.name = jmust
判断依据
/**
* Created by LK on 2016/6/26.
*/
public class MyAutoService {
private String name;
public String sayName(){
return "zzz"+ name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
自动配置
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by LK on 2016/6/26. */ @Configuration @EnableConfigurationProperties(MyAutoProperties.class) @ConditionalOnClass(MyAutoService.class) @ConditionalOnProperty(prefix="app",value = "enabled",matchIfMissing = true) public class MyAutoServiceAutoConfiguration { @Autowired private MyAutoProperties myAutoProperties; @Bean @ConditionalOnMissingBean(MyAutoService.class) public MyAutoService myAutoService(){ MyAutoService myAutoService = new MyAutoService(); myAutoService.setName(myAutoProperties.getName()); return myAutoService; } }
注册配置
如果想自动配置生效,需要注册自动配置类。在src/main/resources 下新建META-INF/spring.factories,在spring.factories中填如下内容注册:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jmust.service.demo.MyAutoServiceAutoConfiguration
使用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by LK on 2016/6/26. */ @RestController @SpringBootApplication public class DemoApplication { @Autowired private MyAutoService myAutoService; @RequestMapping("/") public String index(){ return myAutoService.sayName(); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class,args); } }
在项目中可以直接注入MyAutoService的Bean,但是项目中我们并没有配置这个 Bean,这就是通过自动配置完成的。
效果
访问 http://localhost:8080,就会出现你在application.properties中对应name的内容了。