Spring中的@Value和@PropertySource注解詳解
一、@Value和@PropertySource
1、@Value
@Value注解:為屬性賦值
賦值方式:
- 基本數(shù)值
- SpEl表達(dá)式 #{}
- ${},讀取配置文件[xxx.properties]中的值,配合注解@PropertySource使用
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
/**
* The actual value expression such as <code>#{systemProperties.myProp}</code>
* or property placeholder such as <code>${my.app.myProp}</code>.
*/
String value();
}2、@PropertySource
@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中
@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;
}二、@Value和@PropertySource案例
1、項(xiàng)目結(jié)構(gòu)

2、Person
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
/**
* @Value賦值方式1:基本數(shù)值
*/
@Value("張三")
private String name;
/**
* @Value賦值方式2:SpEl表達(dá)式 #{}
*/
@Value("#{ 20-2 }")
private int age;
/**
* @Value賦值方式3:${},讀取配置文件[xxx.properties]中的值
*/
@Value("${person.phone}")
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
}3、配置類
import org.springframework.context.annotation.*;
/**
* 使用@PropertySource注解可以讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中
*/
@PropertySource(value = {"classpath:/person.properties"},encoding = "UTF-8")
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
}
4、外部文件person.properties
person.phone = 11111111
5、測(cè)試類
import com.dashu.bean.Person;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class Main {
public static void main(String[] args) {
//加載配置類獲取容器
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
//容器中獲取Person
Person person = annotationConfigApplicationContext.getBean(Person.class);
//打印
System.out.println(person);
System.out.println("--------------------");
/**
* 獲取運(yùn)行時(shí)環(huán)境實(shí)例
*/
ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment();
/**
* 根據(jù)外部文件的key,從環(huán)境中獲取value
*/
String property = environment.getProperty("person.phone");
//打印
System.out.println(property);
}
}6、測(cè)試結(jié)果

到此這篇關(guān)于Spring中的@Value和@PropertySource注解詳解的文章就介紹到這了,更多相關(guān)@Value和@PropertySource注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑
下面小編就為大家分享一篇Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)
本篇文章主要介紹了詳解Spring boot Admin 使用eureka監(jiān)控服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)
在目前的企業(yè)級(jí)應(yīng)用開發(fā)中,前后端分離是趨勢(shì),但是視圖層技術(shù)還占有一席之地。Spring Boot 對(duì)視圖層技術(shù)提供了很好的支持,福安防推薦使用的模板引擎是Thymeleaf,不過想FreeMarker也支持,JSP技術(shù)在這里并不推薦使用2022-08-08
如何修改覆蓋spring boot默認(rèn)日志策略logback詳解
這篇文章主要給大家介紹了關(guān)于如何修改覆蓋spring boot默認(rèn)日志策略logback的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

