Spring boot将配置属性注入到bean类中
一、@ConfigurationProperties注解的使用
看配置文件,我的是yaml格式的配置:
//fileapplication.yml my: servers: -dev.bar.com -foo.bar.com -jiaobuchong.com
下面我要将上面的配置属性注入到一个JavaBean类中,看码:
importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.stereotype.Component; importjava.util.ArrayList; importjava.util.List; /** *file:MyConfig.java *Createdbyjiaobuchongon12/29/15. */ @Component//不加这个注解的话,使用@Autowired就不能注入进去了 @ConfigurationProperties(prefix="my")//配置文件中的前缀 publicclassMyConfig{ privateListservers=newArrayList (); publicList getServers(){returnthis.servers; } }
下面写一个Controller来测试一下:
/** *file:HelloController *Createdbyjiaobuchongon2015/12/4. */ @RequestMapping("/test") @RestController publicclassHelloController{ @Autowired privateMyConfigmyConfig; @RequestMapping("/config") publicObjectgetConfig(){ returnmyConfig.getServers(); } }
下面运行Application.java的main方法跑一下看看:
@Configuration//标注一个类是配置类,springboot在扫到这个注解时自动加载这个类相关的功能,比如前面的文章中介绍的配置AOP和拦截器时加在类上的Configuration @EnableAutoConfiguration//启用自动配置该框架就能够进行行为的配置,以引导应用程序的启动与运行,根据导入的starter-pom自动加载配置 @ComponentScan//扫描组件@ComponentScan(value="com.spriboot.controller")配置扫描组件的路径 publicclassApplication{ publicstaticvoidmain(String[]args){ //启动SpringBoot项目的唯一入口 SpringApplicationapp=newSpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); }
在浏览器的地址栏里输入:
localhost:8080/test/config得到:
[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]
二、@ConfigurationProperties和@EnableConfigurationProperties注解结合使用
在springboot中使用yaml进行配置的一般步骤是,
1、yaml配置文件,这里假设:
my: webserver: #HTTP监听端口 port:80 #嵌入Web服务器的线程池配置 threadPool: maxThreads:100 minThreads:8 idleTimeout:60000
2、
//fileMyWebServerConfigurationProperties.java importorg.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix="my.webserver") publicclassMyWebServerConfigurationProperties{ privateintport; privateThreadPoolthreadPool; publicintgetPort(){ returnport; } publicvoidsetPort(intport){ this.port=port; } publicThreadPoolgetThreadPool(){ returnthreadPool; } publicvoidsetThreadPool(ThreadPoolthreadPool){ this.threadPool=threadPool; } publicstaticclassThreadPool{ privateintmaxThreads; privateintminThreads; privateintidleTimeout; publicintgetIdleTimeout(){ returnidleTimeout; } publicvoidsetIdleTimeout(intidleTimeout){ this.idleTimeout=idleTimeout; } publicintgetMaxThreads(){ returnmaxThreads; } publicvoidsetMaxThreads(intmaxThreads){ this.maxThreads=maxThreads; } publicintgetMinThreads(){ returnminThreads; } publicvoidsetMinThreads(intminThreads){ this.minThreads=minThreads; } } }
3、
//file:MyWebServerConfiguration.java importorg.springframework.context.annotation.Configuration; importorg.springframework.boot.context.properties.EnableConfigurationProperties; @Configuration @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) publicclassMyWebServerConfiguration{ @Autowired privateMyWebServerConfigurationPropertiesproperties; /** *下面就可以引用MyWebServerConfigurationProperties类里的配置了 */ publicvoidsetMyconfig(){ Stringport=properties.getPort(); //........... } }
The@EnableConfigurationPropertiesannotationisautomaticallyappliedtoyourprojectsothatanybeansannotatedwith@ConfigurationPropertieswillbeconfiguredfromtheEnvironmentproperties.ThisstyleofconfigurationworksparticularlywellwiththeSpringApplicationexternalYAMLconfiguration.(引自springboot官方手册)
三、@Bean配置第三方组件(Third-partyconfiguration)
创建一个bean类:
//fileThreadPoolBean.java /** *Createdbyjiaobuchongon1/4/16. */ publicclassThreadPoolBean{ privateintmaxThreads; privateintminThreads; privateintidleTimeout; publicintgetMaxThreads(){ returnmaxThreads; } publicvoidsetMaxThreads(intmaxThreads){ this.maxThreads=maxThreads; } publicintgetMinThreads(){ returnminThreads; } publicvoidsetMinThreads(intminThreads){ this.minThreads=minThreads; } publicintgetIdleTimeout(){ returnidleTimeout; } publicvoidsetIdleTimeout(intidleTimeout){ this.idleTimeout=idleTimeout; } }
引用前面第二部分写的配置类:MyWebServerConfiguration.java和MyWebServerConfigurationProperties.java以及yaml配置文件,现在修改MyWebServerConfiguration.java类:
importcom.jiaobuchong.springboot.domain.ThreadPoolBean; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.context.properties.EnableConfigurationProperties; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; /** *Createdbyjiaobuchongon1/4/16. */ @Configuration//这是一个配置类,与@Service、@Component的效果类似。spring会扫描到这个类,@Bean才会生效,将ThreadPoolBean这个返回值类注册到spring上下文环境中 @EnableConfigurationProperties(MyWebServerConfigurationProperties.class)//通过这个注解,将MyWebServerConfigurationProperties这个类的配置到上下文环境中,本类中使用的@Autowired注解注入才能生效 publicclassMyWebServerConfiguration{ @SuppressWarnings("SpringJavaAutowiringInspection")//加这个注解让IDE不报:Couldnotautowire @Autowired privateMyWebServerConfigurationPropertiesproperties; @Bean//@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个bean publicThreadPoolBeangetThreadBean(){ MyWebServerConfigurationProperties.ThreadPoolthreadPool=properties.getThreadPool(); ThreadPoolBeanthreadPoolBean=newThreadPoolBean(); threadPoolBean.setIdleTimeout(threadPool.getIdleTimeout()); threadPoolBean.setMaxThreads(threadPool.getMaxThreads()); threadPoolBean.setMinThreads(threadPool.getMinThreads()); returnthreadPoolBean; } }
被@Configuration注解标识的类,通常作为一个配置类,这就类似于一个xml文件,表示在该类中将配置Bean元数据,其作用类似于Spring里面application-context.xml的配置文件,而@Bean标签,则类似于该xml文件中,声明的一个bean实例。
写一个controller测试一下:
importcom.jiaobuchong.springboot.domain.ThreadPoolBean; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; /** *Createdbyjiaobuchongon2015/12/4. */ @RequestMapping("/first") @RestController publicclassHelloController{ @Autowired privateThreadPoolBeanthreadPoolBean; @RequestMapping("/testbean") publicObjectgetThreadBean(){ returnthreadPoolBean; } }
运行Application.java的main方法,
在浏览器里输入:http://localhost:8080/first/testbean
得到的返回值是:
{“maxThreads”:100,”minThreads”:8,”idleTimeout”:60000}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。