1 簡(jiǎn)單屬性值注入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.xy.test1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service // 需要被注入屬性值的類需要被Spring管理 public class PropertiesService1 { // 利用@Value注解,即使沒(méi)有該屬性或者屬性文件也不會(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); } } |
1
2
3
4
|
#src/main/resource新建文件info.properties name=xy1 num=101 type=1 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!-- 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 = "locations" > < list > < value >classpath:info/info.properties</ value > </ list > </ property > </ bean > |
2 利用util標(biāo)簽注入復(fù)雜屬性值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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注解,即使沒(méi)有該屬性或者屬性文件也不會(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; } } |
1
2
3
4
|
#src/main/resource新建文件info2.properties name=xy2 num=102 type=2 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- 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ì)大家有所幫助。有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。
原文鏈接:http://blog.csdn.net/woshixuye/article/details/54999993