日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java propertysource_[spring] @PropertySource

發布時間:2025/4/16 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java propertysource_[spring] @PropertySource 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

配置文件

@PropertySources注解用于加載配置文件到Spring的環境中。

配置文件如下。

demo.msg=this is a message.

如何引用到配置文件

在app項目中,我們通過@PropertySource注解到JavaConfig類上,設置.properties配置文件的路徑。

在gradle項目中,配置文件放在src/main/resources/路徑下,還可以放在這個目錄下的文件夾。如:src/main/resources/demo/app.properties的設置@PropertySource("demo/app.properties")。

在web項目中,spring web已經將配置文件設置好了,不需要@PropertySource配置。

如何使用配置的值

spring里的許多配置可以在.properties文件中直接配置到。

我們在xml配置,注解等地方需要使用到配置文件的值時,可以使用spring EL語言設置,格式如${x.y.z}。

@PropertySource + @Value

通過在類上設置@PropertySource設置配置文件。

通過在成員變量上設置@Value指定所設置在配置文件中的值。

package com.yww;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;

@Component

@PropertySource(value = "application.properties")

public class Message {

@Value("${demo.msg}")

private String msg;

}

@PropertySource + @ConfigurationProperties

通過在類上設置@PropertySource設置配置文件。

在類上設置@ConfigurationProperties自動將配置文件中名稱滿足的配置值設置。

package com.yww;

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;

import org.springframework.boot.context.properties.ConfigurationProperties;

@Component

@PropertySource(value = "application.properties")

@ConfigurationProperties(prefix = "demo")

public class Message {

private String msg;

}

@ConfigurationProperties是spring boot中的類,需要導入相應的庫。

參考

總結

以上是生活随笔為你收集整理的java propertysource_[spring] @PropertySource的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。