在使用Spring Boot的时候,通常需要自定义一些属性,可以按如下方式直接定义。在src/main/resources/application.properties配置文件中加入:
server.port=8089
com.av.book.name = my spring boot
com.av.book.author = av
然后通过@Value("${属性名}")注解来加载对应的配置属性,具体代码如下:
package com.shrimpking;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 20:35
*/
@Component
@Data
public class BookProperties
{
@Value("${com.av.book.name}")
private String bookName;
@Value("${com.av.book.author}")
private String author;
}
最后,通过单元测试验证BookProperties属性是否已经根据配置文件加载配置:
package com.shrimpking;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 20:37
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyTest
{
@Resource
private BookProperties bookProperties;
@Test
public void test(){
System.out.println("book name:" + bookProperties.getBookName());
System.out.println("book author:" + bookProperties.getAuthor());
}
}
不过我们并不推荐使用这种方式,下面给出更优雅的实现方式。首先引入Spring Boot提供的配置依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shrimpking</groupId>
<artifactId>demo9</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo9</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用@ConfigurationProperties注解进行编码,修改BookProperties为:
package com.shrimpking;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 20:39
*/
@Data
@ConfigurationProperties(prefix = "com.av.book")
public class OtherProperties
{
private String name;
private String author;
}
@ConfigurationProperties(prefix="com.av.book"):在application.properties配置的属性前缀。在类中的属性就不用使用@value进行注入了。
package com.shrimpking;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties({OtherProperties.class})
public class Demo9Application
{
public static void main(String[] args)
{
SpringApplication.run(Demo9Application.class, args);
}
}
最后,在启动类中添加@EnableConfigurationProperties({OtherProperties.class})。