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