一、@ConfigurationProperties注解的使用
看配置文件,我的是yaml格式的配置:
// file application.yml
my:
servers:
- dev.bar.com
- foo.bar.com
- jiaobuchong.com
下面我要将上面的配置属性注入到一个Java Bean类中,看码:
1 import org.springframework.boot.context.properties.ConfigurationProperties; 2 import org.springframework.stereotype.Component; 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8 * file: MyConfig.java 9 * Created by jiaobuchong on 12/29/15. 10 */ 11 @Component //不加这个注解的话, 使用@Autowired 就不能注入进去了 12 @ConfigurationProperties(prefix = "my") // 配置文件中的前缀 13 public class MyConfig { 14 private List<String> servers = new ArrayList<String>(); 15 public List<String> getServers() { return this.servers; 16 } 17 }
下面写一个Controller来测试一下:
1 /** 2 * file: HelloController 3 * Created by jiaobuchong on 2015/12/4. 4 */ 5 @RequestMapping("/test") 6 @RestController 7 public class HelloController { 8 @Autowired 9 private MyConfig myConfig; 10 11 @RequestMapping("/config") 12 public Object getConfig() { 13 return myConfig.getServers(); 14 } 15 }
下面运行Application.java的main方法跑一下看看:
1 @Configuration //标注一个类是配置类,spring boot在扫到这个注解时自动加载这个类相关的功能,比如前面的文章中介绍的配置AOP和拦截器时加在类上的Configuration 2 @EnableAutoConfiguration //启用自动配置 该框架就能够进行行为的配置,以引导应用程序的启动与运行, 根据导入的starter-pom 自动加载配置 3 @ComponentScan //扫描组件 @ComponentScan(value = "com.spriboot.controller") 配置扫描组件的路径 4 public class Application { 5 public static void main(String[] args) { 6 // 启动Spring Boot项目的唯一入口 7 SpringApplication app = new SpringApplication(Application.class); 8 app.setBannerMode(Banner.Mode.OFF); 9 app.run(args); 10 }
在浏览器的地址栏里输入:
localhost:8080/test/config 得到:
[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]
二、@ConfigurationProperties和@EnableConfigurationProperties注解结合使用
在spring boot中使用yaml进行配置的一般步骤是,
1、yaml配置文件,这里假设:
my:
webserver:
#HTTP 监听端口
port: 80
#嵌入Web服务器的线程池配置
threadPool:
maxThreads: 100
minThreads: 8
idleTimeout: 60000
2、
1 //file MyWebServerConfigurationProperties.java 2 import org.springframework.boot.context.properties.ConfigurationProperties; 3 4 @ConfigurationProperties(prefix = "my.webserver") 5 public class MyWebServerConfigurationProperties { 6 private int port; 7 private ThreadPool threadPool; 8 9 public int getPort() { 10 return port; 11 } 12 13 public void setPort(int port) { 14 this.port = port; 15 } 16 17 public ThreadPool getThreadPool() { 18 return threadPool; 19 } 20 21 public void setThreadPool(ThreadPool threadPool) { 22 this.threadPool = threadPool; 23 } 24 25 public static class ThreadPool { 26 private int maxThreads; 27 private int minThreads; 28 private int idleTimeout; 29 30 public int getIdleTimeout() { 31 return idleTimeout; 32 } 33 34 public void setIdleTimeout(int idleTimeout) { 35 this.idleTimeout = idleTimeout; 36 } 37 38 public int getMaxThreads() { 39 return maxThreads; 40 } 41 42 public void setMaxThreads(int maxThreads) { 43 this.maxThreads = maxThreads; 44 } 45 46 public int getMinThreads() { 47 return minThreads; 48 } 49 50 public void setMinThreads(int minThreads) { 51 this.minThreads = minThreads; 52 } 53 } 54 }
3、
1 // file: MyWebServerConfiguration.java 2 import org.springframework.context.annotation.Configuration; 3 import org.springframework.boot.context.properties.EnableConfigurationProperties; 4 5 @Configuration 6 @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) 7 public class MyWebServerConfiguration { 8 @Autowired 9 private MyWebServerConfigurationProperties properties; 10 /** 11 *下面就可以引用MyWebServerConfigurationProperties类 里的配置了 12 */ 13 public void setMyconfig() { 14 String port = properties.getPort(); 15 // ........... 16 } 17 }
The @EnableConfigurationProperties annotation is automatically applied to your project so that any beans annotated with @ConfigurationProperties will be configured from the Environment properties. This style of configuration works particularly well with the SpringApplication external YAML configuration.(引自spring boot官方手册)
三、@Bean配置第三方组件(Third-party configuration)
创建一个bean类:
1 // file ThreadPoolBean.java 2 /** 3 * Created by jiaobuchong on 1/4/16. 4 */ 5 public class ThreadPoolBean { 6 private int maxThreads; 7 private int minThreads; 8 private int idleTimeout; 9 10 public int getMaxThreads() { 11 return maxThreads; 12 } 13 14 public void setMaxThreads(int maxThreads) { 15 this.maxThreads = maxThreads; 16 } 17 18 public int getMinThreads() { 19 return minThreads; 20 } 21 22 public void setMinThreads(int minThreads) { 23 this.minThreads = minThreads; 24 } 25 26 public int getIdleTimeout() { 27 return idleTimeout; 28 } 29 30 public void setIdleTimeout(int idleTimeout) { 31 this.idleTimeout = idleTimeout; 32 } 33 }
引用前面第二部分写的配置类:MyWebServerConfiguration.java和MyWebServerConfigurationProperties.java以及yaml配置文件,现在修改MyWebServerConfiguration.java类:
1 import com.jiaobuchong.springboot.domain.ThreadPoolBean; 2 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.boot.context.properties.EnableConfigurationProperties; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 7 /** 8 * Created by jiaobuchong on 1/4/16. 9 */ 10 @Configuration //这是一个配置类,与@Service、@Component的效果类似。spring会扫描到这个类,@Bean才会生效,将ThreadPoolBean这个返回值类注册到spring上下文环境中 11 @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) //通过这个注解, 将MyWebServerConfigurationProperties这个类的配置到上下文环境中,本类中使用的@Autowired注解注入才能生效 12 public class MyWebServerConfiguration { 13 @SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire 14 @Autowired 15 private MyWebServerConfigurationProperties properties; 16 17 @Bean //@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个bean 18 public ThreadPoolBean getThreadBean() { 19 MyWebServerConfigurationProperties.ThreadPool threadPool = properties.getThreadPool(); 20 ThreadPoolBean threadPoolBean = new ThreadPoolBean(); 21 threadPoolBean.setIdleTimeout(threadPool.getIdleTimeout()); 22 threadPoolBean.setMaxThreads(threadPool.getMaxThreads()); 23 threadPoolBean.setMinThreads(threadPool.getMinThreads()); 24 return threadPoolBean; 25 } 26 }
被@Configuration注解标识的类,通常作为一个配置类,这就类似于一个xml文件,表示在该类中将配置Bean元数据,其作用类似于Spring里面application-context.xml的配置文件,而@Bean标签,则类似于该xml文件中,声明的一个bean实例。
写一个controller测试一下:
1 import com.jiaobuchong.springboot.domain.ThreadPoolBean; 2 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 /** 7 * Created by jiaobuchong on 2015/12/4. 8 */ 9 @RequestMapping("/first") 10 @RestController 11 public class HelloController { 12 @Autowired 13 private ThreadPoolBean threadPoolBean; 14 @RequestMapping("/testbean") 15 public Object getThreadBean() { 16 return threadPoolBean; 17 } 18 19 }
运行Application.java的main方法,
在浏览器里输入:http://localhost:8080/first/testbean
得到的返回值是:
{“maxThreads”:100,”minThreads”:8,”idleTimeout”:60000}
ok,fucking nice