Spring boot將配置屬性注入到bean類中
一、@ConfigurationProperties注解的使用
看配置文件,我的是yaml格式的配置:
// file application.yml my: servers: - dev.bar.com - foo.bar.com - jiaobuchong.com
下面我要將上面的配置屬性注入到一個(gè)Java Bean類中,看碼:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * file: MyConfig.java * Created by jiaobuchong on 12/29/15. */ @Component //不加這個(gè)注解的話, 使用@Autowired 就不能注入進(jìn)去了 @ConfigurationProperties(prefix = "my") // 配置文件中的前綴 public class MyConfig { private List<String> servers = new ArrayList<String>(); public List<String> getServers() { return this.servers; } }
下面寫一個(gè)Controller來(lái)測(cè)試一下:
/** * file: HelloController * Created by jiaobuchong on 2015/12/4. */ @RequestMapping("/test") @RestController public class HelloController { @Autowired private MyConfig myConfig; @RequestMapping("/config") public Object getConfig() { return myConfig.getServers(); } }
下面運(yùn)行Application.java的main方法跑一下看看:
@Configuration //標(biāo)注一個(gè)類是配置類,spring boot在掃到這個(gè)注解時(shí)自動(dòng)加載這個(gè)類相關(guān)的功能,比如前面的文章中介紹的配置AOP和攔截器時(shí)加在類上的Configuration @EnableAutoConfiguration //啟用自動(dòng)配置 該框架就能夠進(jìn)行行為的配置,以引導(dǎo)應(yīng)用程序的啟動(dòng)與運(yùn)行, 根據(jù)導(dǎo)入的starter-pom 自動(dòng)加載配置 @ComponentScan //掃描組件 @ComponentScan(value = "com.spriboot.controller") 配置掃描組件的路徑 public class Application { public static void main(String[] args) { // 啟動(dòng)Spring Boot項(xiàng)目的唯一入口 SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); }
在瀏覽器的地址欄里輸入:
localhost:8080/test/config 得到:
[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]
二、@ConfigurationProperties和@EnableConfigurationProperties注解結(jié)合使用
在spring boot中使用yaml進(jìn)行配置的一般步驟是,
1、yaml配置文件,這里假設(shè):
my: webserver: #HTTP 監(jiān)聽(tīng)端口 port: 80 #嵌入Web服務(wù)器的線程池配置 threadPool: maxThreads: 100 minThreads: 8 idleTimeout: 60000
2、
//file MyWebServerConfigurationProperties.java import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "my.webserver") public class MyWebServerConfigurationProperties { private int port; private ThreadPool threadPool; public int getPort() { return port; } public void setPort(int port) { this.port = port; } public ThreadPool getThreadPool() { return threadPool; } public void setThreadPool(ThreadPool threadPool) { this.threadPool = threadPool; } public static class ThreadPool { private int maxThreads; private int minThreads; private int idleTimeout; public int getIdleTimeout() { return idleTimeout; } public void setIdleTimeout(int idleTimeout) { this.idleTimeout = idleTimeout; } public int getMaxThreads() { return maxThreads; } public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; } public int getMinThreads() { return minThreads; } public void setMinThreads(int minThreads) { this.minThreads = minThreads; } } }
3、
// file: MyWebServerConfiguration.java import org.springframework.context.annotation.Configuration; import org.springframework.boot.context.properties.EnableConfigurationProperties; @Configuration @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) public class MyWebServerConfiguration { @Autowired private MyWebServerConfigurationProperties properties; /** *下面就可以引用MyWebServerConfigurationProperties類 里的配置了 */ public void setMyconfig() { String port = properties.getPort(); // ........... } }
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官方手冊(cè))
三、@Bean配置第三方組件(Third-party configuration)
創(chuàng)建一個(gè)bean類:
// file ThreadPoolBean.java /** * Created by jiaobuchong on 1/4/16. */ public class ThreadPoolBean { private int maxThreads; private int minThreads; private int idleTimeout; public int getMaxThreads() { return maxThreads; } public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; } public int getMinThreads() { return minThreads; } public void setMinThreads(int minThreads) { this.minThreads = minThreads; } public int getIdleTimeout() { return idleTimeout; } public void setIdleTimeout(int idleTimeout) { this.idleTimeout = idleTimeout; } }
引用前面第二部分寫的配置類:MyWebServerConfiguration.java和MyWebServerConfigurationProperties.java以及yaml配置文件,現(xiàn)在修改MyWebServerConfiguration.java類:
import com.jiaobuchong.springboot.domain.ThreadPoolBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by jiaobuchong on 1/4/16. */ @Configuration //這是一個(gè)配置類,與@Service、@Component的效果類似。spring會(huì)掃描到這個(gè)類,@Bean才會(huì)生效,將ThreadPoolBean這個(gè)返回值類注冊(cè)到spring上下文環(huán)境中 @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) //通過(guò)這個(gè)注解, 將MyWebServerConfigurationProperties這個(gè)類的配置到上下文環(huán)境中,本類中使用的@Autowired注解注入才能生效 public class MyWebServerConfiguration { @SuppressWarnings("SpringJavaAutowiringInspection") //加這個(gè)注解讓IDE 不報(bào): Could not autowire @Autowired private MyWebServerConfigurationProperties properties; @Bean //@Bean注解在方法上,返回值是一個(gè)類的實(shí)例,并聲明這個(gè)返回值(返回一個(gè)對(duì)象)是spring上下文環(huán)境中的一個(gè)bean public ThreadPoolBean getThreadBean() { MyWebServerConfigurationProperties.ThreadPool threadPool = properties.getThreadPool(); ThreadPoolBean threadPoolBean = new ThreadPoolBean(); threadPoolBean.setIdleTimeout(threadPool.getIdleTimeout()); threadPoolBean.setMaxThreads(threadPool.getMaxThreads()); threadPoolBean.setMinThreads(threadPool.getMinThreads()); return threadPoolBean; } }
被@Configuration注解標(biāo)識(shí)的類,通常作為一個(gè)配置類,這就類似于一個(gè)xml文件,表示在該類中將配置Bean元數(shù)據(jù),其作用類似于Spring里面application-context.xml的配置文件,而@Bean標(biāo)簽,則類似于該xml文件中,聲明的一個(gè)bean實(shí)例。
寫一個(gè)controller測(cè)試一下:
import com.jiaobuchong.springboot.domain.ThreadPoolBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by jiaobuchong on 2015/12/4. */ @RequestMapping("/first") @RestController public class HelloController { @Autowired private ThreadPoolBean threadPoolBean; @RequestMapping("/testbean") public Object getThreadBean() { return threadPoolBean; } }
運(yùn)行Application.java的main方法,
在瀏覽器里輸入:http://localhost:8080/first/testbean
得到的返回值是:
{“maxThreads”:100,”minThreads”:8,”idleTimeout”:60000}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Java中實(shí)現(xiàn)可見(jiàn)性(visibility)的主要方法詳解
這篇文章主要介紹了在Java中實(shí)現(xiàn)可見(jiàn)性(visibility)的主要方法詳解,在Java中,使用關(guān)鍵字volatile和使用鎖(如synchronized關(guān)鍵字或 java.util.concurrent包中的鎖)來(lái)確保對(duì)共享變量的修改在多線程環(huán)境中能夠正確地被其他線程所觀察到,需要的朋友可以參考下2023-08-08Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫(kù)詳解
這篇文章主要為大家詳細(xì)介紹了Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫(kù)的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06SpringBoot項(xiàng)目中忽略某屬性返回?cái)?shù)據(jù)給前端
在Spring Boot中,保護(hù)敏感信息和減少數(shù)據(jù)傳輸是很重要的,我們可以使用多種方法來(lái)忽略返回?cái)?shù)據(jù)中的字段,無(wú)論是使用@JsonIgnore注解、Projection投影、@JsonIgnoreProperties注解還是自定義序列化器,都能達(dá)到我們的目的,在實(shí)際應(yīng)用中,根據(jù)具體場(chǎng)景和需求選擇合適的方法2024-05-05Java圖書管理系統(tǒng),課程設(shè)計(jì)必用(源碼+文檔)
本系統(tǒng)采用Java,MySQL 作為系統(tǒng)數(shù)據(jù)庫(kù),重點(diǎn)開(kāi)發(fā)并實(shí)現(xiàn)了系統(tǒng)各個(gè)核心功能模塊,包括采編模塊、典藏模塊、基礎(chǔ)信息模塊、流通模塊、期刊模塊、查詢模塊、評(píng)論模塊、系統(tǒng)統(tǒng)計(jì)模塊以及幫助功能模塊2021-06-06javaDSL簡(jiǎn)單實(shí)現(xiàn)示例分享
DSL領(lǐng)域定義語(yǔ)言,用來(lái)描述特定領(lǐng)域的特定表達(dá)。比如畫圖從起點(diǎn)到終點(diǎn);路由中的從A到B。這是關(guān)于畫圖的一個(gè)簡(jiǎn)單實(shí)現(xiàn)2014-03-03Java 隊(duì)列 Queue 用法實(shí)例詳解
本文實(shí)例講述了Java內(nèi)置隊(duì)列類Queue用法,分享給大家供大家參考2017-04-04Java多線程CountDownLatch的實(shí)現(xiàn)
本文主要介紹了Java多線程CountDownLatch的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02