bean:
- 在spring技術中是基于組件的
- 最基本了是最常用的單元
- 其實實例保存在spring的容器當中
bean通常被定義在配置文件當中,bean實例化由spring的ioc容器進行管理,bean的實例可以通過beanfactory進行訪問,實際上大部分j2ee應用,bean是通過applicationcontext來訪問的,applicationcontext是beanfactory的子接口,功能要比beanfactory強大許多
在前面得博客依賴注入與控制反轉中演示了應用spring實現ioc,在applicationcontext.xml中有bean的配置,里面只是bean簡單的應用。這篇主要是進一步學習使用bean。
一、定義
1
2
3
4
5
6
7
8
9
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd"> <bean id= "daoimpl" class = "cuiyw.spring.dao.daoimpl" ></bean> <bean id= "serviceimpl" class = "cuiyw.spring.service.serviceimpl" scope= "singleton" > <property name= "dao" ref= "daoimpl" ></property> </bean> </beans> |
上面的代碼是之前博客配置的,可以看到bean的基本構成。 <beans/>
是sring配置文件的根節點,一個<beans/>
節點里面可以有多個<bean>
節點。在bean中常用兩個屬性:id,class. id是一唯一標識,來確定是哪個bean,可以讓其他bean中使用id引用。class用來指定是哪個class。同時還可以設置scope屬性,scope有兩種:singleton,non-singelton。singleton:單實例模式(默認,構造方法為private),整個spring的容器中只有一個共享實例存在(singleton)。non-singelton:每次請求該bean,spring容器都會新建立一個bean實例,然后返回給程序(request,session,prototype)。
二、創建
bean的命名機制
id 當在spring的窗口當中,查找某個bean對象時,首先根據id進行查找,將其余作為bean的默認名稱,如果id屬性不存在,則根據name屬性進行查找(將其中的第一個名稱作為默認的名稱),如果id和name都不存在根據類的名稱進行查找。id---------->name--------------->類名。
bean的別名:可以使用alias來為bean指定別名.
下面的配置文件還是在上面的xml基礎上修改的。這里配置了id為serviceimpl的bean設置了別名。我們可以使用它的name、id、alias來獲取service。
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd"> <bean id= "daoimpl" class = "cuiyw.spring.dao.daoimpl" ></bean> <bean id= "serviceimpl" class = "cuiyw.spring.service.serviceimpl" scope= "singleton" name= "servicea" > <property name= "dao" ref= "daoimpl" ></property> </bean> <alias name= "servicea" alias= "servicea1" /> </beans> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package cuiyw.springaop; import org.springframework.beans.factory.beanfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import cuiyw.spring.iservice.iservice; public class app { public static void main( string[] args ) { applicationcontext context= new classpathxmlapplicationcontext( new string[]{ "applicationcontext.xml" }); beanfactory factory=context; iservice service=(iservice)factory.getbean( "servicea1" ); service.service( "cuiyw servicea1" ); service=(iservice)factory.getbean( "servicea" ); service.service( "cuiyw servicea" ); service=(iservice)factory.getbean( "serviceimpl" ); service.service( "cuiyw serviceimpl" ); } } |
三、注入
1.基本類型和string
可以使用value元素來設置,在上面的代碼基礎上稍作修改
1
|
<property name= "baseproperty" value= "222" ></property> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package cuiyw.spring.service; import cuiyw.spring.idao.idao; import cuiyw.spring.iservice.iservice; public class serviceimpl implements iservice{ private idao dao; private int baseproperty; public idao getdao() { return dao; } public void setdao(idao dao) { this .dao = dao; } public void service(string name) { system.out.println(dao.sayhello(name)+ " baseproperty:" +getbaseproperty()); } public int getbaseproperty() { return baseproperty; } public void setbaseproperty( int baseproperty) { this .baseproperty = baseproperty; } } |
對于string類型,xml解析器以string類型解析出數據,如果屬性不是string類型,屬性值會通過propertyeditors轉換為其他類型,比如時間類型.
2.注入bean
上面的代碼中就注入了bean,在serviceimpl中注入daoimpl。可以使用ref來進行配置。
3.注入集合
常見的集合有list、map、set、property等,下面的代碼是在serviceimpl中定義了幾個屬性,然后在上下文中通過屬性注入進去。為了測試,在daoimpl中也增加了一個屬性s。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package cuiyw.spring.dao; import java.util.calendar; import cuiyw.spring.idao.idao; public class daoimpl implements idao{ public string s; public string gets() { return s; } public void sets(string s) { this .s = s; } public string sayhello(string name) { int hour=calendar.getinstance().get(calendar.hour_of_day); if (hour< 6 ) return "凌晨早," +name; if (hour< 12 ) return "早上好," +name; if (hour< 13 ) return "中午好," +name; if (hour< 18 ) return "下午好," +name; return "晚上好," +name+ ", s=" +s; } } |
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package cuiyw.spring.service; import java.util.iterator; import java.util.list; import java.util.map; import java.util.properties; import java.util.set; import cuiyw.spring.idao.idao; import cuiyw.spring.iservice.iservice; public class serviceimpl implements iservice{ private idao dao; private int baseproperty; private list<object> lists; private set<object> sets; private map<object, object> maps; private properties pros; public idao getdao() { return dao; } public void setdao(idao dao) { this .dao = dao; } public void service(string name) { system.out.println(dao.sayhello(name)+ ",baseproperty:" +getbaseproperty()); for ( int i= 0 ;i<lists.size();i++) { object obj=lists.get(i); system.out.println(obj.getclass().getname()); } for (object obj : sets) { system.out.println(obj.getclass().getname()); } //遍歷maps中的key for (object key : maps.keyset()) { system.out.println( "key = " + key); } //遍歷maps中的值 for (object value : maps.values()) { system.out.println( "value = " + value); } set<string> pro=pros.stringpropertynames(); iterator<string> it=pro.iterator(); while (it.hasnext()){ object key=it.next(); system.out.println(key+ "----" +pros.getproperty((string) key)); } } public int getbaseproperty() { return baseproperty; } public void setbaseproperty( int baseproperty) { this .baseproperty = baseproperty; } public list<object> getlists() { return lists; } public void setlists(list<object> lists) { this .lists = lists; } public set<object> getsets() { return sets; } public void setsets(set<object> sets) { this .sets = sets; } public map<object, object> getmaps() { return maps; } public void setmaps(map<object, object> maps) { this .maps = maps; } public properties getpros() { return pros; } public void setpros(properties pros) { this .pros = pros; } } |
主要是注入的配置,在list、map、set屬性中都是配置了一個基礎類型value=1,兩個daoimpl類型。
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
37
38
39
40
41
42
43
44
45
46
47
48
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd"> <bean id= "daoimpl" class = "cuiyw.spring.dao.daoimpl" > <property name= "s" value= "cyw" ></property> </bean> <bean id= "serviceimpl" class = "cuiyw.spring.service.serviceimpl" scope= "singleton" name= "servicea" > <property name= "dao" ref= "daoimpl" ></property> <property name= "baseproperty" value= "222" ></property> <property name= "lists" > <list> <value> 1 </value> <ref bean= "daoimpl" /> <bean class = "cuiyw.spring.dao.daoimpl" > <property name= "s" value= "cuiywlists" /> </bean> </list> </property> <property name= "sets" > <set> <value> 1 </value> <ref bean= "daoimpl" /> <bean class = "cuiyw.spring.dao.daoimpl" > <property name= "s" value= "cuiywsets" /> </bean> </set> </property> <property name= "maps" > <map> <entry key= "key1" value= "1" ></entry> <entry key= "key2" value-ref= "daoimpl" ></entry> <entry key= "key3" > <bean class = "cuiyw.spring.dao.daoimpl" > <property name= "s" value= "cuiywmaps" /> </bean> </entry> </map> </property> <property name= "pros" > <props> <prop key= "prokey1" >prokeya</prop> <prop key= "prokey2" >prokeyb</prop> </props> </property> </bean> <alias name= "servicea" alias= "servicea1" /> </beans> |
執行main方法可以看到屬性都注入進去了。
4.自定義屬性編輯器
對于有一些屬性是沒法注入的,此時就需要自定義,比如上面說的日期類型。
首先是要定義一個繼承propertyeditorsupport的類,重寫setastext方法。
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
|
package cuiyw.spring.service; import java.beans.propertyeditorsupport; import java.text.parseexception; import java.text.simpledateformat; public class customerproperty extends propertyeditorsupport { private string format= "yyyy-mm-dd" ; public string getformat() { return format; } public void setformat(string format) { this .format = format; } @override public void setastext(string text) throws illegalargumentexception { // todo auto-generated method stub simpledateformat sdf= new simpledateformat(format); //super.setastext(text); try { //轉換對象,能過setvalue方法重新賦值 this .setvalue(sdf.parse(text)); } catch (parseexception e) { e.printstacktrace(); } } } |
然后在配置文件配置這個類
1
2
3
4
5
6
7
|
<bean id= "customeditorconfigurer" class = "org.springframework.beans.factory.config.customeditorconfigurer" > <property name= "customeditors" > <map> <entry key= "java.util.date" value= "cuiyw.spring.service.customerproperty" /> </map> </property> </bean> |
這里還是在serviceimpl中增加了一個java.util.date
類型的date屬性。并在配置文件注入值。
1
|
<property name= "date" value= "2017-12-10" /> |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/5ishare/p/7967104.html