[JAVA] [Spring Boot] I want to add my own property file and get the value with env.getProperty ().

Demo app configuration

デモアプリ構成.png src/main/resources/test.properties Property file added by. Get the value from this file. プロパティファイル内.png The state of ↑ in the property file. # Add property file to Environment with @PropertySource ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.PropertySource;

@SpringBootApplication @PropertySource("classpath:test.properties") public class PropertyTestApplication {

public static void main(String[] args) {
	SpringApplication.run(PropertyTestApplication.class, args);
}

}

 You can add it to Environment by specifying the path of the property file in the argument of @PropertySource ().

 @PropertySource must be declared with @Configuration,
 Since @Configuration is declared in @SpringBootApplication, there is no need to declare it specially.

 [Click here for @PropertySource reference](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html)

# Get it with env.getProperty ().
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class PropertyGetComponent {

	@Autowired
	Environment env;

	public void printProperty() {
		String value = env.getProperty("test.property.key");
		System.out.println("The obtained value is[ " + value + " ]is.");
	}

}

The execution result of printProperty () is ↓

The obtained value is[ test value ]is.

I was able to get the value from the property file added by env.getProperty (): blush:

Referenced articles

Recommended Posts

[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
Procedure to make the value of the property file visible in Spring Boot
[Spring Boot] How to refer to the property file
I want to get the value in Ruby
Extract SQL to property file with jdbcTemplate of spring boot
I tried to get started with Swagger using Spring Boot
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
I want to get a list of the contents of a zip file and its uncompressed size
I want to control the maximum file size in file upload for each URL in Spring Boot
I want to return to the previous screen with kotlin and java!
I want to control the default error message of Spring Boot
Add spring boot and gradle to eclipse
I want to display images with REST Controller of Java and Spring!
[Ruby] I want to extract only the value of the hash and only the key
I used Docker to solidify the template to be developed with spring boot.
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
How to get the setting value (property value) from the database in Spring Framework
[Java Spring MVC] I want to use DI in my own class
How to create your own annotation in Java and get the value
I want to download a file on the Internet using Ruby and save it locally (with caution)
How to bind to property file in Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
I want to control the start / stop of servers and databases with Alexa
I want to recursively get the superclass and interface of a certain class
I tried to implement file upload with Spring MVC
05. I tried to stub the source of Spring Boot
I tried to get started with Spring Data JPA
I want to dark mode with the SWT app
I want to transition screens with kotlin and java!
Attempt to SSR Vue.js with Spring Boot and GraalJS
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to get along with Map [Java beginner]
Form and process file and String data at the same time with Spring Boot + Java
I want to add a delete function to the comment function
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
Connect to database with spring boot + spring jpa and CRUD operation
I want to put the JDK on my Mac PC
I want to distinct the duplicated data with has_many through
I want to implement various functions with kotlin and java!
I want to pass the startup command to postgres with docker-compose.
Get started with Spring boot
File upload with Spring Boot
Add module with Spring Boot
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
I want to give edit and delete permissions only to the poster
I want to hook log file generation / open with log4j # FileAppender
I want to add a browsing function with ruby on rails
I want to understand the flow of Spring processing request parameters
I want to INSERT Spring Local Time with MySQL Time (also milliseconds)
How to create your own Controller corresponding to / error with Spring Boot
[Eclipse] I want to open the same file twice [Split editor]
What I did in the migration from Spring Boot 1.4 series to 2.0 series
Method to add the number of years and get the end of the month
I want to add the disabled option to f.radio_button depending on the condition
What I did in the migration from Spring Boot 1.5 series to 2.0 series
I wanted to make JavaFX programming easier with the Spring Framework