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 use Properties in Spring

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 "how to use Properties in Spring". In daily operation, I believe many people have doubts about how to use Properties in Spring. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Properties in Spring"! Next, please follow the editor to study!

The use of Properties

Readers of this article have all used Spring. Let's first take a look at how Properties is used. There are several common ways to use Spring:

1. Used in xml configuration files

That is, automatically replace the value in ${}.

two。 Used by @ Value injection

@ Value ("${javadoop.jdbc.url}")

Private String url

3. Get it through Environment

There are some points that should be paid attention to in this law. Not all configuration methods support obtaining attribute values through the Environment API. In personal testing, you can only use the annotation @ PropertySource, otherwise you will get null. As for how to configure it, we will talk about it in a minute.

@ Autowired

Private Environment env

Public String getUrl () {

Return env.getProperty ("javadoop.jdbc.url")

}

If it is registered by Spring Boot's application.properties, it is also possible.

Properties configuration

We talked about how to use the Properties we configured earlier, so how to configure it? Spring provides a variety of configuration methods.

1. Configure via xml

The following is the most commonly used configuration, as many projects say:

one

two。 Configure through @ PropertySource

The previous configuration via xml is very common, but if you also have an impulse to destroy all xml configuration files, you should use the following ways:

@ PropertySource ("classpath:sys.properties")

@ Configuration

Public class JavaDoopConfig {

}

Note that @ PropertySource must be used with @ Configuration here, not to mention it.

3. PropertyPlaceholderConfigurer

Readers should not be surprised if they have seen this, as it was often used before Spring 3.1:

Classpath:sys.properties

Of course, we can also use the corresponding version of java configuration:

@ Bean

Public PropertyPlaceholderConfigurer propertiess () {

PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ()

Resource [] resources = new ClassPathResource [] {new ClassPathResource ("sys.properties")}

Ppc.setLocations (resources)

Ppc.setIgnoreUnresolvablePlaceholders (true)

Return ppc

}

4. PropertySourcesPlaceholderConfigurer

When it comes to Spring 3.1, PropertySourcesPlaceholderConfigurer is introduced, which is a new class. Note that there is a Sources in the name of the previous PropertyPlaceholderConfigurer, and it belongs to a different package, which is in the Spring-Context package.

There is no difference in configuration:

Classpath:sys.properties

Let's also have a java configuration version:

@ Bean

Public PropertySourcesPlaceholderConfigurer properties () {

PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer ()

Resource [] resources = new ClassPathResource [] {new ClassPathResource ("sys.properties")}

Pspc.setLocations (resources)

Pspc.setIgnoreUnresolvablePlaceholders (true)

Return pspc

}

Spring Boot correlation

Spring Boot is really a good thing, and it feels so good to use it right out of the box. Here is a brief introduction to the relevant content.

Quickly generate a Spring Boot project: https://start.spring.io/

Application.properties

We have an application.properties file by default for each project. This configuration file does not need to be registered as mentioned earlier. Spring Boot will help us register automatically.

Of course, maybe it's okay if you want to change your name, just specify your file name at startup:

1java-Dspring.config.location=classpath:sys.properties-jar app.jar

Application- {env} .properties

We will use this in order to specify different configurations for different environments.

For example, the database connection information is different between the test environment and the production environment.

So, on top of application.properties, we also need to create a new application-dev.properties and application-prd.properties to configure the environment-related information, and then specify the environment at startup.

1java-Dspring.profiles.active=prd-jar app.jar

As a result, the configuration in both the application.properties and application-prd.properties files is registered, and if there is a duplicate key,application-prd.properties file, the priority is higher.

@ ConfigurationProperties

This comment is only available in Spring Boot.

Even if you do not use this annotation, you may see this in open source projects. Here is a brief introduction.

Let's give an example to be more intuitive. As mentioned earlier, fill the configuration file with the following information, and you can choose to write to application.properties or use the method described in the first section.

Javadoop.database.url=jdbc:mysql:

Javadoop.database.username=admin

Javadoop.database.password=admin123456

Java file:

@ Configuration

@ ConfigurationProperties (prefix = "javadoop.database")

Public class DataBase {

String url

String username

String password

/ / getters and setters

}

In this way, a bean of type DataBase is automatically registered in the container of Spring, and the properties are all set.

Dynamically modify attribute values during startup

I don't think we need much introduction to this. Those who use Spring Boot should know it basically.

Property configuration has an override order, that is, where the value will prevail when the same key occurs.

Startup parameters > application- {env} .properties > application.properties

Startup parameters dynamically set properties:

1java-Djavadoop.database.password=admin4321-jar app.jar

In addition, you can also use system environment variables to set properties, you can also specify random numbers, and so on, which is indeed very flexible, but it is useless and will not be introduced.

At this point, the study on "how to use Properties in Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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