Spring中利用配置文件和@value注入屬性值代碼詳解
1 簡(jiǎn)單屬性值注入
package com.xy.test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service // 需要被注入屬性值的類需要被Spring管理
public class PropertiesService1 {
// 利用@Value注解,即使沒有該屬性或者屬性文件也不會(huì)報(bào)錯(cuò)
// @Value輸入屬性值name,默認(rèn)值xydefault
@Value("${name:xydefault}")
private String name;
// @Value輸入屬性值num,默認(rèn)值-1
@Value("${num:-1}")
private Integer num;
// @Value輸入屬性值type,默認(rèn)值-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文件 --> <!-- 掃描測(cè)試屬性包中的類,要注入屬性類需要被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標(biāo)簽注入復(fù)雜屬性值
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注解,即使沒有該屬性或者屬性文件也不會(huì)報(bào)錯(cuò)
*/
@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 --> <!-- 掃描測(cè)試屬性包中的類,要注入屬性類需要被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>
總結(jié)
以上就是本文關(guān)于Spring中利用配置文件和@value注入屬性值代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。
相關(guān)文章
詳解Spring Boot 定時(shí)任務(wù)的實(shí)現(xiàn)方法
最近在用SpringBoot寫一個(gè)關(guān)于定時(shí)項(xiàng)目的時(shí)候遇到一個(gè)問題,下面小編把如何處理定時(shí)任務(wù)的解決思路分享給大家 ,需要的朋友參考下2017-05-05
Java超詳細(xì)講解WebMvcConfigurer攔截器
這篇文章將用實(shí)例來(lái)和大家介紹一下WebMvcConfigurer攔截器。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-06-06
Java提示解析時(shí)已到達(dá)文件結(jié)尾的解決方法
在本篇文章中小編給大家分享了關(guān)于Java提示解析時(shí)已到達(dá)文件結(jié)尾的解決方法,需要的朋友們學(xué)習(xí)下。2019-07-07
在IDEA中如何設(shè)置最多顯示文件標(biāo)簽個(gè)數(shù)
在使用IDEA進(jìn)行編程時(shí),可能會(huì)同時(shí)打開多個(gè)文件,當(dāng)文件過(guò)多時(shí),文件標(biāo)簽會(huì)占據(jù)大部分的IDEA界面,影響我們的編程效率,因此,我們可以通過(guò)設(shè)置IDEA的文件標(biāo)簽顯示個(gè)數(shù),來(lái)優(yōu)化我們的編程環(huán)境,具體的設(shè)置方法如下2024-10-10
Java并發(fā)底層實(shí)現(xiàn)原理學(xué)習(xí)心得
本片文章是學(xué)習(xí)Java并發(fā)底層實(shí)現(xiàn)原理的一篇知識(shí)心得,對(duì)大家學(xué)習(xí)這個(gè)方便的知識(shí)很有幫助,一起參考下。2018-01-01

