最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring boot將配置屬性注入到bean類中

 更新時間:2017年03月02日 11:13:17   作者:heikeyuit  
本篇文章主要介紹了Spring boot將配置屬性注入到bean類中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、@ConfigurationProperties注解的使用

看配置文件,我的是yaml格式的配置:

// file application.yml
my:
 servers:
  - dev.bar.com
  - foo.bar.com
  - jiaobuchong.com

下面我要將上面的配置屬性注入到一個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   //不加這個注解的話, 使用@Autowired 就不能注入進(jìn)去了
@ConfigurationProperties(prefix = "my") // 配置文件中的前綴
public class MyConfig {
  private List<String> servers = new ArrayList<String>();
  public List<String> getServers() { return this.servers;
  }
}

下面寫一個Controller來測試一下:

/**
 * 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)注一個類是配置類,spring boot在掃到這個注解時自動加載這個類相關(guān)的功能,比如前面的文章中介紹的配置AOP和攔截器時加在類上的Configuration


@EnableAutoConfiguration //啟用自動配置 該框架就能夠進(jìn)行行為的配置,以引導(dǎo)應(yīng)用程序的啟動與運(yùn)行, 根據(jù)導(dǎo)入的starter-pom 自動加載配置
@ComponentScan //掃描組件 @ComponentScan(value = "com.spriboot.controller") 配置掃描組件的路徑
public class Application {
  public static void main(String[] args) {
    // 啟動Spring Boot項目的唯一入口
    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)聽端口
  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官方手冊)

三、@Bean配置第三方組件(Third-party configuration)

創(chuàng)建一個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 //這是一個配置類,與@Service、@Component的效果類似。spring會掃描到這個類,@Bean才會生效,將ThreadPoolBean這個返回值類注冊到spring上下文環(huán)境中
@EnableConfigurationProperties(MyWebServerConfigurationProperties.class) //通過這個注解, 將MyWebServerConfigurationProperties這個類的配置到上下文環(huán)境中,本類中使用的@Autowired注解注入才能生效
public class MyWebServerConfiguration {
  @SuppressWarnings("SpringJavaAutowiringInspection") //加這個注解讓IDE 不報: Could not autowire
  @Autowired
  private MyWebServerConfigurationProperties properties;

  @Bean //@Bean注解在方法上,返回值是一個類的實(shí)例,并聲明這個返回值(返回一個對象)是spring上下文環(huán)境中的一個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)識的類,通常作為一個配置類,這就類似于一個xml文件,表示在該類中將配置Bean元數(shù)據(jù),其作用類似于Spring里面application-context.xml的配置文件,而@Bean標(biāo)簽,則類似于該xml文件中,聲明的一個bean實(shí)例。
寫一個controller測試一下:

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}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java 中程序內(nèi)存的分析

    詳解Java 中程序內(nèi)存的分析

    這篇文章主要介紹了詳解Java 中程序內(nèi)存的分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • springboot配置文件綁定實(shí)現(xiàn)解析

    springboot配置文件綁定實(shí)現(xiàn)解析

    這篇文章主要介紹了springboot配置文件綁定實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼

    SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼

    這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java線程池ForkJoinPool實(shí)例解析

    Java線程池ForkJoinPool實(shí)例解析

    這篇文章主要介紹了Java線程池ForkJoinPool實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • 代碼實(shí)例Java IO判斷目錄和文件是否存在

    代碼實(shí)例Java IO判斷目錄和文件是否存在

    本篇文章給大家分享了Java IO判斷目錄和文件是否存在的代碼,對此有需要的讀者們可以跟著小編一起學(xué)習(xí)下。
    2018-02-02
  • 值得Java程序猿閱讀的書籍

    值得Java程序猿閱讀的書籍

    這篇文章主要推薦了一些值得Java程序猿閱讀的書籍,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • springboot處理url中帶斜杠/\字符的參數(shù)報400問題

    springboot處理url中帶斜杠/\字符的參數(shù)報400問題

    這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報400問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java中絕對值函數(shù)的介紹與其妙用

    Java中絕對值函數(shù)的介紹與其妙用

    這篇文章主要給大家介紹了Java中絕對值函數(shù)的介紹與其妙用,其中包括絕對值函數(shù)用來獲取表達(dá)式的絕對值和絕對值函數(shù)實(shí)現(xiàn)降序+升序輸出。文章末尾給出了實(shí)例介紹,有需要的朋友們可以參考學(xué)習(xí),下面來一起看看吧。
    2017-01-01
  • 詳解如何在React中逃離閉包陷阱

    詳解如何在React中逃離閉包陷阱

    眾所周知,JavaScript 中的閉包(Closures)一定是這種語言最可怕的特性之一,另外它可能也是最隱蔽的語言特性之一,我們在編寫 React 代碼時經(jīng)常會用到它,但是大多數(shù)時候我們甚至沒有意識到這一點(diǎn),本文小編將和大家一起深入探討如何在React中逃離閉包陷阱
    2023-09-09
  • SpringCloud?Feign使用ApacheHttpClient代替默認(rèn)client方式

    SpringCloud?Feign使用ApacheHttpClient代替默認(rèn)client方式

    這篇文章主要介紹了SpringCloud?Feign使用ApacheHttpClient代替默認(rèn)client方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論

涿鹿县| 达孜县| 白沙| 成都市| 邮箱| 金川县| 澳门| 孝义市| 甘孜县| 宁河县| 鄂伦春自治旗| 射洪县| 碌曲县| 堆龙德庆县| 旬邑县| 合江县| 龙山县| 浠水县| 理塘县| 从化市| 康平县| 中西区| 得荣县| 彭泽县| 吴忠市| 门源| 黎平县| 沾化县| 微山县| 宜兴市| 大理市| 丰县| 奇台县| 长葛市| 明星| 京山县| 于都县| 故城县| 封开县| 拉萨市| 阿拉善左旗|