springboot 自定義屬性與加載@value示例詳解
在使用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屬性是否已經(jīng)根據(jù)配置文件加載配置:
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());
}
}不過我們并不推薦使用這種方式,下面給出更優(yōu)雅的實現(xiàn)方式。首先引入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})。
到此這篇關于springboot 自定義屬性與加載@value的文章就介紹到這了,更多相關springboot 加載@value內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows系統(tǒng)下Eclipse搭建ESP32編譯環(huán)境及安裝過程
Ecppse 使用了 ESP-IDF 中的 Makefile 支持。這意味著您需要從創(chuàng)建 ESP-IDF 項目開始。您可以使用 github 中的 idf-template 項目,接下來通過本文給大家介紹Windows系統(tǒng)下Eclipse搭建ESP32編譯環(huán)境及安裝過程,感興趣的朋友一起看看吧2021-10-10
Java Spring Security認證與授權及注銷和權限控制篇綜合解析
Spring Security 是 Spring 家族中的一個安全管理框架,實際上,在 Spring Boot 出現(xiàn)之前,Spring Security 就已經(jīng)發(fā)展了多年了,但是使用的并不多,安全管理這個領域,一直是 Shiro 的天下2021-10-10

