Spring boot injects configuration properties into bean classes

Spring boot injects configuration properties into bean classes

following:https://blog.csdn.net/jiaobuchong/article/details/50442709

1. Use of @ConfigurationProperties annotation


Looking at the configuration file, mine is the configuration in yaml format:

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

 

Next, I want to inject the above configuration properties into a Java Bean class, see the code:

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 就不能注入进去了 @ConfigurationProperties(prefix = "my") // 配置文件中的前缀 public class MyConfig { private List<String> servers = new ArrayList<String>(); public List<String> getServers() { return this.servers; } }

   
   

Let's write a Controller to test it:

/**
 * 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(); } }

 

Let's run the main method of Application.java and run it:

@Configuration   //标注一个类是配置类,spring boot在扫到这个注解时自动加载这个类相关的功能,比如前面的文章中介绍的配置AOP和拦截器时加在类上的Configuration
@EnableAutoConfiguration  //启用自动配置 该框架就能够进行行为的配置,以引导应用程序的启动与运行, 根据导入的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); }

Enter in the browser's address bar: 
localhost:8080/test/config and get: 
["dev.bar.com","foo.bar.com","jiaobuchong.com"] 

Second, @ConfigurationProperties and @EnableConfigurationProperties annotations are used in combination


The general steps for using yaml for configuration in spring boot are, 
1. yaml configuration file, here is assumed: 

my:
  webserver:
    #HTTP 监听端口
    port: 80
    #嵌入Web服务器的线程池配置
    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官方手册) 

Third, @Bean configuration third-party components (Third-party configuration)

Create a bean class:

// 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; } }

Referring to the configuration classes written in the second part above: MyWebServerConfiguration.java and MyWebServerConfigurationProperties.java and the yaml configuration file, now modify the MyWebServerConfiguration.java class:

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上下文环境中 @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) //通过这个注解, 将MyWebServerConfigurationProperties这个类的配置到上下文环境中,本类中使用的@Autowired注解注入才能生效 public class MyWebServerConfiguration { @SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire @Autowired private MyWebServerConfigurationProperties properties; @Bean //@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个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; } }

The class identified by the @Configuration annotation is usually used as a configuration class, which is similar to an xml file, indicating that the bean metadata will be configured in this class, and its role is similar to the configuration file of application-context.xml in Spring, and @ The Bean tag is similar to a bean instance declared in the xml file. 
Write a controller to test it:

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; } }


Run the main method of Application.java 
and enter in the browser: http://localhost:8080/first/testbean 
The return value is: 
{"maxThreads":100,"minThreads":8,"idleTimeout":60000} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600155&siteId=291194637