Spring中利用配置文件和@value注入屬性值代碼詳解
更新時間:2017年11月17日 16:40:30 作者:woshixuye
這篇文章主要介紹了Spring中利用配置文件和@value注入屬性值代碼詳解,代碼中注釋比較詳細,具有一定參考價值,需要的朋友可以了解下。
1 簡單屬性值注入
package com.xy.test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service // 需要被注入屬性值的類需要被Spring管理
public class PropertiesService1 {
// 利用@Value注解,即使沒有該屬性或者屬性文件也不會報錯
// @Value輸入屬性值name,默認值xydefault
@Value("${name:xydefault}")
private String name;
// @Value輸入屬性值num,默認值-1
@Value("${num:-1}")
private Integer num;
// @Value輸入屬性值type,默認值-2
@Value("${type:-2}")
private Integer type;
public void getInfo() {
System.out.println("name:" + name + ",num:" + num + ",type:" + type);
}
}
#src/main/resource新建文件info.properties name=xy1 num=101 type=1
<!-- applicationContext.xml文件 --> <!-- 掃描測試屬性包中的類,要注入屬性類需要被Spring管理 --> <context:component-scan base-package="com.xy.test1" /> <!--方法1--> <!-- <context:property-placeholder location="classpath*:info/info.properties" /> --> <!--方法2--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1" /> <property name="locations"> <list> <value>classpath:info/info.properties</value> </list> </property> </bean>
2 利用util標簽注入復雜屬性值
package com.xy.test2;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* 該類必須被Spring容器管理屬性才可以被注入。利用@Value注解,即使沒有該屬性或者屬性文件也不會報錯
*/
@Service
public class PropertiesService2 {
@Value("#{testPro}")
private Properties pros;
@Value("#{testList}")
private List<String> myList;
@Value("#{testMap}")
private Map<Integer, String> myMap;
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Map<Integer, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<Integer, String> myMap) {
this.myMap = myMap;
}
}
#src/main/resource新建文件info2.properties name=xy2 num=102 type=2
<!-- applicationContext.xml --> <!-- 掃描測試屬性包中的類,要注入屬性類需要被Spring管理 --> <context:component-scan base-package="com.xy.test2" /> <!-- properties --> <util:properties id="testPro" location="classpath:info/info2.properties" /> <!-- list --> <util:list id="testList" list-class="java.util.ArrayList"> <value>first</value> <value>second</value> <value>third</value> </util:list> <!-- map --> <util:map id="testMap" map-class="java.util.HashMap"> <entry key="1" value="first" /> <entry key="2" value="second" /> <entry key="3" value="third" /> </util:map>
總結
以上就是本文關于Spring中利用配置文件和@value注入屬性值代碼詳解的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,小編會及時回復大家的。
相關文章
詳解Spring 框架中切入點 pointcut 表達式的常用寫法
這篇文章主要介紹了詳解Spring 框架中切入點 pointcut 表達式的常用寫法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
@Autowired自動裝配,@Bean注入@Primary,@Qualifier優(yōu)先級講解
這篇文章主要介紹了@Autowired自動裝配,@Bean注入@Primary,@Qualifier優(yōu)先級,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java 線程對比(Thread,Runnable,Callable)實例詳解
這篇文章主要介紹了Java 線程(Thread,Runnable,Callable)實例詳解的相關資料,這里對java 線程的三種方法進行了對比,需要的朋友可以參考下2016-12-12

