Spring boot injects configuration properties into bean classes

1、 Use of @ configurationproperties annotation

Look at the configuration file. My configuration is in yaml format:

Next, I want to inject the above configuration attributes into a java bean class. Look at the code:

Write a controller to test:

Now run application Run the main method of Java to see:

Enter in the address bar of the browser:

Localhost: 8080 / test / config:

[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]

2、 @ configurationproperties is used in conjunction with the @ enableconfigurationproperties annotation

The general steps for configuring spring boot using yaml are:,

1. Yaml configuration file, here it is assumed that:

2、

3、

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. (quoted from the official spring boot manual)

3、 @ bean third party configuration

Create a bean class:

Reference the configuration class written in the second part above: mywebserverconfiguration Java and mywebserverconfigurationproperties Java and yaml configuration files. Now modify mywebserverconfiguration Java class:

The class identified by @ configuration annotation is usually used as a configuration class, which is similar to an XML file, indicating that bean metadata will be configured in this class, and its function is similar to application context in spring XML configuration file, and the @ bean tag is similar to a bean instance declared in the XML file. Write a controller to test:

Run application Java's main method,

Enter in the browser: http://localhost:8080/first/testbean

The returned value is:

{“maxThreads”:100,”minThreads”:8,”idleTimeout”:60000}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>