Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Spring Boot configuration files

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the Spring Boot configuration files are, which are introduced in great detail and have certain reference value. Friends who are interested must finish reading them!

I. Custom attributes

When we create a springboot project, an application.properties is created for us under the src/main/java/resources directory by default. Personal habit, I will change application.properties to application.yml file, both file formats are supported.

Customize a set of properties in application.yml:

My: name: forezp age: 12

If you need to read the value of the configuration file, just add @ Value ("${property name}"):

@ RestControllerpublic class MiyaController {@ Value ("${my.name}") private String name; @ Value ("${my.age}") private int age; @ RequestMapping (value = "/ miya") public String miya () {return name+ ":" + age;}}

Start the project, visit: localhost:8080/miya, the browser displays:

Forezp:12

Assign the attributes of the configuration file to the entity class

When we have a lot of configuration properties, we create a javabean with these properties as fields and assign property values to them, such as:

My: name: forezp age: 12 number: ${random.int} uuid: ${random.uuid} max: ${random.int (10)} value: ${random.value} greeting: hi,i'm ${my.name}

${random} is used in the configuration file, which can be used to generate different types of random values.

How to assign these attributes to a javabean, first create a javabean:

@ ConfigurationProperties (prefix = "my") @ Componentpublic class ConfigBean {private String name; private int age; private int number; private String uuid; private int max; private String value; private String greeting; omitted getter setter....

You need to add an annotation @ ConfigurationProperties and add its prrfix. In addition, @ Component can be added or not. In addition, spring-boot-configuration-processor dependence can be added or not, the specific reasons are unknown.

Org.springframework.boot spring-boot-configuration-processor true

In addition, you need to add EnableConfigurationProperties comments to the application class or application class.

@ RestController@EnableConfigurationProperties ({ConfigBean.class}) public class LucyController {@ Autowired ConfigBean configBean; @ RequestMapping (value = "/ lucy") public String miya () {return configBean.getGreeting () + ">" + configBean.getName () + ">" + configBean.getUuid () + ">" + configBean.getMax ();}

Start the project, visit localhost:8080/lucy, and we will find that the configuration file information has been read.

III. Custom configuration file

What is described above is that we all write the configuration file to application.yml. Sometimes we don't want to write all the configurations into the application configuration file, so we need to customize the configuration file, such as test.properties:

Com.forezp.name=forezpcom.forezp.age=12

How do you assign this profile information to a javabean?

Configuration@PropertySource (value = "classpath:test.properties") @ ConfigurationProperties (prefix = "com.forezp") public class User {private String name; private int age; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;}}

In the latest version of springboot, you need to add these three comments. @ Configuration

@ PropertySource (value = "classpath:test.properties")

@ ConfigurationProperties (prefix = "com.forezp"); required in version 1.4

PropertySource plus location.

@ RestController@EnableConfigurationProperties ({ConfigBean.class,User.class}) public class LucyController {@ Autowired ConfigBean configBean; @ RequestMapping (value = "/ lucy") public String miya () {return configBean.getGreeting () + ">" + configBean.getName () + ">" + configBean.getUuid () + ">" + configBean.getMax ();} @ Autowired User user RequestMapping (value = "/ user") public String user () {return user.getName () + user.getAge ();}}

Start the project, open the localhost:8080/user; browser and display:

Forezp12

IV. Multiple environment profiles

In a real development environment, we need different configuration environments; the format is application- {profile} .properties, where {profile} corresponds to your environment ID, such as:

Application-test.properties: test environment

Application-dev.properties: development environment

Application-prod.properties: production environment

How do I use it? We just need to add the following to the application.yml:

Spring: profiles: active: dev

Where application-dev.yml:

Server: port: 8082

Start the project and find that the port of the program is no longer 8080, but 8082.

These are all the contents of the article "what are the Spring Boot configuration files?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report