[JAVA] [Spring Boot] How to refer to the property file

Preface

I had a hard time not knowing how to refer to the property file in Spring Boot, so I wrote it as an article. I think that you can understand it if you have enough knowledge to "Hello World" with Spring Boot.

Source and commentary

Prepare property file

Prepare the property file in src / main / resources through the classpath. The name of the property file can be anything.

hogeConfig.properties


hoge.foo=foooooooo
hoge.bar=barrrrrrr

Prepare a bean for the property file

Add @Component to make it a Bean (a class that can be Autowired). Specify the name of the property file with @PropertySource. Specify the property prefix with @ConfigurationProperties. Describe variables, setters and getters.

HogeConfig.java


@Component
@PropertySource("classpath:hogeConfig.properties")
@ConfigurationProperties(prefix = "hoge")
public class HogeConfig {
    private String foo;
	//setter getter omitted
}

Refer to the value of the property

Prepare the Controller and Service classes. Inject the bean prepared earlier with @Autowired. Call the getter of the injected bean.

HogeController.java


@RestController
public class HogeController {
	@Autowired
    private HogeConfig hogeConfig;

    @GetMapping("/hoge")
    public String hoge() {
    	return hogeConfig.getFoo(); // foooooooo
    }
}

reference

Spring @PropertySource example Type-safe property settings in @ConfigurationProperties of Spring Boot Why Spring recommends Constructor Injection over Field Injection

Recommended Posts

[Spring Boot] How to refer to the property file
How to bind to property file in Spring Boot
How to split Spring Boot message file
Procedure to make the value of the property file visible in Spring Boot
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
How to set environment variables in the properties file of Spring boot application
Extract SQL to property file with jdbcTemplate of spring boot
[Java] How to use the File class
How to delete the wrong migration file
How to delete the migration file NO FILE
[Spring Boot] I investigated how to implement post-processing of the received request.
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
The story of raising Spring Boot 1.5 series to 2.1 series
How to add a classpath in Spring Boot
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
How to get the setting value (property value) from the database in Spring Framework
Spring Boot --How to set session timeout time
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
[Java] How to extract the file name from the path
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[Spring Boot] How to create a project (for beginners)
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to debug the generated jar file in Eclipse
Deploy the Spring Boot project to Tomcat on XAMPP
Try Spring Boot from 0 to 100.
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
Deploy Spring Boot applications to Heroku without using the Heroku CLI
Deploy the application created by Spring Boot to Heroku (public) ②
How to call and use API in Java (Spring Boot)
Deploy the application created by Spring Boot to Heroku (public) ①
How to change the contents of the jar file without decompressing
How to change the file name with Xcode (Refactor Rename)
How to control transactions in Spring Boot without using @Transactional
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
I want to control the maximum file size in file upload for each URL in Spring Boot
How to use the link_to method
Add Extended Attributes to the file
How to use the include? method
How to unit test Spring AOP
How to use the form_with method
Spring Boot for the first time
How to use Spring Data JDBC
How to find the average angle
[How to install Spring Data Jpa]
How to convert erb file to haml
Upgrade spring boot from 1.5 series to 2.0 series
[Beginner] How to delete NO FILE
How to add the delete function
How to create a jar file or war file using the jar command