When using Spring Boot, you usually need to customize some properties, which can be defined directly as follows. In the src/main/resources/ configuration file:
=8089 = my spring boot = av
Then load the corresponding configuration attributes through the @Value("${attribute name}") annotation. The specific code is as follows:
package ; import ; import ; import ; /** * Created by IntelliJ IDEA. * * @Author : Shrimpking * @create 2024/1/13 20:35 */ @Component @Data public class BookProperties { @Value("${}") private String bookName; @Value("${}") private String author; }
Finally, verify that the BookProperties attribute has loaded the configuration according to the configuration file through unit tests:
package ; import ; import ; import ; import ; import .; import ; /** * Created by IntelliJ IDEA. * * @Author : Shrimpking * @create 2024/1/13 20:37 */ @SpringBootTest @RunWith() public class MyTest { @Resource private BookProperties bookProperties; @Test public void test(){ ("book name:" + ()); ("book author:" + ()); } }
However, we do not recommend this method, and the following is a more elegant implementation method. First, we introduce the configuration dependencies provided by Spring Boot:
<dependency> <groupId></groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <parent> <groupId></groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId></groupId> <artifactId>demo9</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo9</name> <description>Demo project for Spring Boot</description> <properties> <>1.8</> </properties> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId></groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId></groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
Use the @ConfigurationProperties annotation for encoding, and modify BookProperties to:
package ; import ; import ; /** * Created by IntelliJ IDEA. * * @Author : Shrimpking * @create 2024/1/13 20:39 */ @Data @ConfigurationProperties(prefix = "") public class OtherProperties { private String name; private String author; }
@ConfigurationProperties(prefix=""): Prefix in the configuration property. Attributes in the class do not need to be injected using @value.
package ; import ; import ; import ; @SpringBootApplication @EnableConfigurationProperties({}) public class Demo9Application { public static void main(String[] args) { (, args); } }
Finally, add @EnableConfigurationProperties({}) to the startup class.
This is the article about springboot custom properties and loading @value. For more related springboot loading @value, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!