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

How to read custom properties configuration file under SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to read a custom properties configuration file under SpringBoot". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to read a custom properties configuration file under SpringBoot" can help you solve the problem.

First, create a new .properties file in resource

Create a new config folder under the resource directory, and then create a new .properties file under that folder. As shown in figure remote.properties

Second, prepare configuration files

Remote.uploadFilesUrl=/resource/files/remote.uploadPicUrl=/resource/pic/

3. Create a new configuration class RemoteProperties.java

@ Configuration@ConfigurationProperties (prefix = "remote", ignoreUnknownFields = false) @ PropertySource ("classpath:config/remote.properties") @ Data@Componentpublic class RemoteProperties {private String uploadFilesUrl; private String uploadPicUrl;}

Among them

@ Configuration indicates that this is a configuration class

@ ConfigurationProperties (prefix = "remote", ignoreUnknownFields = false) this annotation is used to bind attributes. Prefix is used to select the prefix of the attribute, that is, the "remote" in the remote.properties file, and ignoreUnknownFields is used to tell SpringBoot to throw an exception if an attribute does not match the declared domain.

@ PropertySource ("classpath:config/remote.properties") profile path

@ Data this is a lombok annotation used to generate the getter&setter method

@ Component is identified as Bean

How to use it?

Annotate EnableConfigurationProperties (RemoteProperties.class) on the table on the class where you want to use the configuration file

And automatically injected.

@ AutowiredRemoteProperties remoteProperties

You can get the configuration content by using remoteProperties.getUploadFilesUrl () in the method.

@ EnableConfigurationProperties (RemoteProperties.class) @ RestControllerpublic class TestService {@ Autowired RemoteProperties remoteProperties; public void test () {String str = remoteProperties.getUploadFilesUrl (); System.out.println (str);}}

Here str is the "/ resource/files/" in the configuration file.

PS: let's take a look at two ways to read the config configuration file in Spring-boot

Anyone who is familiar with the technology of spring-Boot should know the core configuration file application.properties of Spring-Boot. Of course, you can also annotate the information of custom configuration files.

How Spring-Boot reads the configuration file:

one。 Read the contents of the core configuration file information application.properties

The core configuration file refers to the application.properties or application.yml configuration file under the resources root directory. There are two simple ways to read these two configuration files.

The core configuration file application.properties contains the following:

Test.msg=Hello World SpringBoot

Method 1: use @ Value mode (commonly used)

Package Solin.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @ RestController public class WebController {@ Value ("${test.msg}") private String msg; @ RequestMapping ("/ index1") public String index1 () {return "method 1:" + msg;}}

Note: @ Value's ${} contains the key name from the core configuration file. Adding @ RestController to the Controller class means that all views in this class are displayed as JSON, similar to adding @ ResponseBody to the view method.

When visiting http://localhost:8088/index1, you get the following: "method 1: Hello World SpringBoot"

Method 2: use Environment mode

Package Solin.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @ RestController public class WebController {@ Autowired private Environment env; @ RequestMapping ("/ index2") public String index2 () {return "method 2:" + env.getProperty ("test.msg");}}

Note: this is done by relying on injection Evnironment. Add @ Autowired annotation to the created member variable private Environment env to complete dependency injection, and then use env.getProperty ("key name") to read the corresponding value.

When visiting http://localhost:8088/index2, you get the following: "method 2: Hello World SpringBoot"

two。 Read custom profile information, such as author.properties

In order not to destroy the original ecology of the core file, but also requires the existence of custom configuration information, generally, you will choose a custom configuration file to put these custom information. Here, create the configuration file author.properties in the resources directory.

The resources/author.properties content is as follows:

Author.name=Solin author.age=22

Create an entity class to manage the configuration:

Package Solin.controller; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; / / with the annotation @ Component, you can directly use @ Autowired elsewhere to create its instance object @ Component @ ConfigurationProperties (prefix = "author", locations = "classpath:author.properties") public class MyWebConfig {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;}}

Note:

There are two properties in the @ ConfigurationProperties comment:

Locations: specify the location of the configuration file

Prefix: specifies the prefix of the key name in the configuration file (all the key names in my configuration file are author. Beginning)

The use of @ Component allows the class to be relied upon elsewhere, that is, to create an instance using the @ Autowired annotation.

Create a test Controller

Package Solin.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @ Controller public class ConfigController {@ Autowired private MyWebConfig conf; @ RequestMapping ("/ test") public @ ResponseBody String test () {return "Name:" + conf.getName () + "-" + "Age:" + conf.getAge ();}}

Note: since @ Component is annotated on the Conf class, you can directly use @ Autowired here to create its instance object.

When visiting http://localhost:8088/test, you get "Name:Solin---Age:22".

This is the end of the introduction to "how to read a custom properties configuration file under SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report