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、測試類
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)行時環(huán)境實(shí)例 */ ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment(); /** * 根據(jù)外部文件的key,從環(huán)境中獲取value */ String property = environment.getProperty("person.phone"); //打印 System.out.println(property); } }
6、測試結(jié)果
到此這篇關(guān)于Spring中的@Value和@PropertySource注解詳解的文章就介紹到這了,更多相關(guān)@Value和@PropertySource注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法
這篇文章主要介紹了IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法
這篇文章主要介紹了SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法,SpringMVC讀取Cookie判斷用戶是否登錄,對每一個action都要進(jìn)行判斷,有興趣的可以了解一下。2017-03-03IDEA自帶Maven插件找不到settings.xml配置文件
IDEA自帶了Maven插件,最近發(fā)現(xiàn)了一個問題,IDEA自帶Maven插件找不到settings.xml配置文件,本文就來詳細(xì)的介紹一下解決方法,感興趣的可以了解一下2023-11-11Java11中基于嵌套關(guān)系的訪問控制優(yōu)化詳解
Java(和其他語言)通過內(nèi)部類支持嵌套類,要使其正常工作,需要編譯器執(zhí)行一些技巧,下面這篇文章主要給大家介紹了關(guān)于Java11中基于嵌套關(guān)系的訪問控制優(yōu)化的相關(guān)資料,需要的朋友可以參考下2022-01-01CommonMark 使用教程:將 Markdown 語法轉(zhuǎn)成 Html
這篇文章主要介紹了CommonMark 使用教程:將 Markdown 語法轉(zhuǎn)成 Html,這個技巧我們做任何網(wǎng)站都可以用到,而且非常好用。,需要的朋友可以參考下2019-06-06java實(shí)現(xiàn)微信點(diǎn)餐申請微信退款
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信點(diǎn)餐申請微信退款,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09基于Java實(shí)現(xiàn)簡單的身材計(jì)算程序
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)簡單的身材計(jì)算程序,可以計(jì)算身體的體脂率以及BMI數(shù)值等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12