Spring中使用@Value注解注入屬性文件中的值詳解
@Value注解注入屬性文件中的值
通過(guò)Spring的@Value注解可以將xml中關(guān)聯(lián)的屬性文件中的值注入變量中,這樣就不需要通過(guò)創(chuàng)建Properties然后根據(jù)屬性文件讀取屬性值了。
1、定義一個(gè)屬性文件
首先定義一個(gè)屬性文件,其中存儲(chǔ)代碼中需要引入的屬性值

2、添加一個(gè)配置文件
spring-config-properties.xml,通過(guò)spring容器讀取,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:properties id="configProperties" location="classpath:conf/setting.properties" />
</beans>3、測(cè)試類
編寫(xiě)測(cè)試類測(cè)試注入的效果:
package com.teriste.other;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:conf/spring/spring-config-properties.xml"})
public class PropertiesTest {
@Value("#{configProperties['secretAccessKey']}")
private String secretAccessKey;
public String getSecretAccessKey() {
return secretAccessKey;
}
public void setSecretAccessKey(String secretAccessKey) {
this.secretAccessKey = secretAccessKey;
}
@Test
public void testGetProperties(){
System.out.println("通過(guò)@Value注解注入屬性文件中的值:"+secretAccessKey);
}
}4、測(cè)試結(jié)果

到此這篇關(guān)于Spring中使用@Value注解注入屬性文件中的值詳解的文章就介紹到這了,更多相關(guān)@Value注解注入屬性文件中的值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)易連連看小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易連連看小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
基于request獲取訪問(wèn)者真實(shí)IP代碼示例
這篇文章主要介紹了基于request獲取訪問(wèn)者真實(shí)IP代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Spring(二):Spring通過(guò)IOC來(lái)創(chuàng)建對(duì)象
下面小編就為大家?guī)?lái)一篇詳談Spring對(duì)IOC的理解(推薦篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-07-07
Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式
這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

