springboot 自定義屬性與加載@value示例詳解
在使用Spring Boot的時(shí)候,通常需要自定義一些屬性,可以按如下方式直接定義。在src/main/resources/application.properties配置文件中加入:
server.port=8089 com.av.book.name = my spring boot com.av.book.author = av
然后通過@Value("${屬性名}")注解來加載對應(yīng)的配置屬性,具體代碼如下:
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;
}最后,通過單元測試驗(yàn)證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)雅的實(shí)現(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注解進(jìn)行編碼,修改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進(jìn)行注入了。
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})。
到此這篇關(guān)于springboot 自定義屬性與加載@value的文章就介紹到這了,更多相關(guān)springboot 加載@value內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
java中循環(huán)遍歷刪除List和Set集合中元素的方法(推薦)
下面小編就為大家?guī)硪黄猨ava中循環(huán)遍歷刪除List和Set集合中元素的方法(推薦)。小編覺得挺不錯的,在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
詳解JAVA中的Collection接口和其主要實(shí)現(xiàn)的類
這篇文章主要介紹了JAVA中的Collection接口和其主要實(shí)現(xiàn)的類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
深入理解 CAS 算法原理已經(jīng)在jdk中的運(yùn)用
這篇文章主要介紹了深入理解 CAS 算法原理已經(jīng)在jdk中的運(yùn)用,幫助大家更好的使用Java,感興趣的朋友可以了解下2020-12-12
Spring Security 實(shí)現(xiàn)用戶名密碼登錄流程源碼詳解
在服務(wù)端的安全管理使用了Spring Security,用戶登錄成功之后,Spring Security幫你把用戶信息保存在Session里,但是具體保存在哪里,要是不深究你可能就不知道,今天小編就帶大家具體了解一下Spring Security實(shí)現(xiàn)用戶名密碼登錄的流程2021-11-11
MyBatis-Flex BaseMapper的接口基本用法小結(jié)
本文主要介紹了MyBatis-Flex BaseMapper的接口基本用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié))
這篇文章主要介紹了一文詳解Spring任務(wù)執(zhí)行和調(diào)度(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

