Spring中@PropertySource配置的用法
組合使用,可以將自定義屬性文件中的屬性變量值注入到當前類的使用@Value注解的成員變量中,需要的朋友可以參考下
@PropertySource配置的用法
功能
- 加載指定的屬性文件(*.properties)到 Spring 的 Environment 中??梢耘浜?@Value 和 @ConfigurationProperties 使用。
- @PropertySource 和 @Value組合使用,可以將自定義屬性文件中的屬性變量值注入到當前類的使用@Value注解的成員變量中。
- @PropertySource 和 @ConfigurationProperties組合使用,可以將屬性文件與一個Java類綁定,將屬性文件中的變量值注入到該Java類的成員變量中。
源碼
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.io.support.PropertySourceFactory;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
/**
* 屬性源的名稱
*/
String name() default "";
/**
* 屬性文件的存放路徑
*/
String[] value();
/**
* 如果指定的屬性源不存在,是否要忽略這個錯誤
*/
boolean ignoreResourceNotFound() default false;
/**
* 屬性源的編碼格式
*/
String encoding() default "";
/**
* 屬性源工廠
*/
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
使用示例
屬性文件:demo.properties
demo.name=huang demo.sex=1 demo.type=demo
示例一:@PropertySource + @Value
package com.huang.pims.demo.props;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
public class ReadByPropertySourceAndValue {
@Value("${demo.name}")
private String name;
@Value("${demo.sex}")
private int sex;
@Value("${demo.type}")
private String type;
@Override
public String toString() {
return "ReadByPropertySourceAndValue{" +
"name='" + name + '\'' +
", sex=" + sex +
", type='" + type + '\'' +
'}';
}
}
示例二:@PropertySource 和 @ConfigurationProperties
package com.huang.pims.demo.props;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
@ConfigurationProperties(prefix = "demo")
public class ReadByPropertySourceAndConfProperties {
private String name;
private int sex;
private String type;
public void setName(String name) {
this.name = name;
}
public void setSex(int sex) {
this.sex = sex;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public int getSex() {
return sex;
}
public String getType() {
return type;
}
@Override
public String toString() {
return "ReadByPropertySourceAndConfProperties{" +
"name='" + name + '\'' +
", sex=" + sex +
", type='" + type + '\'' +
'}';
}
}
示例測試
package com.huang.pims.demo.runners;
import com.huang.pims.demo.props.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class OutputPropsRunner implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class);
@Autowired
private ReadByPropertySourceAndValue readByPropertySourceAndValue;
@Autowired
private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties;
@Override
public void run(String... args) throws Exception {
LOGGER.info(readByPropertySourceAndValue.toString());
LOGGER.info(readByPropertySourceAndConfProperties.toString());
}
}
啟動項目即可看到效果。

從截圖中可以看出,需要讀取的屬性配置,都已經(jīng)成功讀取出來了。
到此這篇關于Spring中@PropertySource配置的用法的文章就介紹到這了,更多相關@PropertySource配置的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java 數(shù)據(jù)結構與算法系列精講之時間復雜度與空間復雜度
對于一個算法,其時間復雜度和空間復雜度往往是相互影響的,當追求一個較好的時間復雜度時,可能會使空間復雜度的性能變差,即可能導致占用較多的存儲空間,這篇文章主要給大家介紹了關于Java時間復雜度、空間復雜度的相關資料,需要的朋友可以參考下2022-02-02
java返回前端樹形結構數(shù)據(jù)的2種實現(xiàn)方式
近期項目有個需求,需要將組織機構數(shù)據(jù)拼成樹型結構返回至前端,下面這篇文章主要給大家介紹了關于java返回前端樹形結構數(shù)據(jù)的2種實現(xiàn)方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-05-05
Spring data jpa @Query update的坑及解決
這篇文章主要介紹了Spring data jpa @Query update的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

