0%

JavaBean的实例化与注入方式

实例化Bean的方式

spring.xml 样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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-->
<bean id="bean" class="com.?.Bean"/>
<!--通过静态方法实例化Bean-->
<bean id="beanFromFactory1" class="com.***.BeanFactory" factory-method="createBean1"/>
<!--通过实例方法实例化Bean-->
<bean id="beanFactory" class="com.***.BeanFactory"/>
<bean id="beanFromFactory2" factory-bean="beanFactory" factory-method="createBean2"/>
<!--Bean的别名-->
<bean id="bean1" name="bean2, bean3" class="com.***.Bean"/>
<alias name="bean1" alias="bean4"/>

</beans>

Spring DI(依赖注入)

spring.xml 样例

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?xml version="1.0" encoding="UTF-8"?>
<!--使用"c:"."p:"需要引入"xmlns:c"与"xmlns:p",这两个链接访问不到,但是可以正常使用,很不解-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="anotherBean" class="com.***.AnotherBean"/>

<!-- 通过构造方法注入Bean,"c:"为简便写法-->
<bean id="diByConstructor1" class="com.***.DIByConstructor">
<!-- 一般来说index或name就可以唯一确定,所以两者中只用写一个,而type可以省略-->
<!-- index:构造函数中参数的位置-->
<!-- type:参数类名,基本类型填包装类-->
<!-- name:type填写的Bean的构造函数中的参数的名字-->
<!-- value:常用类型填值-->
<!-- ref:自定义类型填值,这里的值为所填type类Bean的id-->
<constructor-arg index="0" name="anotherBean"
type="com.***.AnotherBean"
ref="anotherBean"/>
<constructor-arg index="1" name="stringValue"
type="java.lang.String"
value="aaaa"/>
<constructor-arg index="2" name="integerValue"
type="java.lang.Integer"
value="1111"/>
</bean>

<bean id="diByConstructor2" class="com.***.DIByConstructor"
c:anotherBean-ref="anotherBean"
c:stringValue="aaaa"
c:integerValue="1111"/>

<!-- 通过set方法注入Bean,"p:"为简便写法-->
<bean id="diBySetter1" class="com.***.DIBySetter">
<!--name:Bean中变量名-->
<!-- value:常用类型填值-->
<!-- ref:自定义类型填值,这里的值为所填type类Bean的id-->
<property name="anotherBean" ref="anotherBean"/>
<property name="stringValue" value="aaaa"/>
<property name="integerValue" value="1111"/>
</bean>

<bean id="diBySetter2" class="com.***.DIBySetter"
p:anotherBean-ref="anotherBean"
p:stringValue="aaaa"
p:integerValue="1111"/>

<bean id="anotherBean1" class="com.***.AnotherBean"/>
<bean id="anotherBean2" class="com.***.AnotherBean"/>
<bean id="anotherBean3" class="com.***.AnotherBean"/>
<bean id="anotherBean4" class="com.***.AnotherBean"/>
<bean id="anotherBean5" class="com.***.AnotherBean"/>
<bean id="anotherBean6" class="com.***.AnotherBean"/>
<bean id="anotherBean7" class="com.***.AnotherBean"/>
<bean id="anotherBean8" class="com.***.AnotherBean"/>
<bean id="diByCollection" class="com.***.DIByCollection">

<!-- list -->
<property name="stringList">
<list>
<value>aaaaa</value>
<value>bbbbb</value>
</list>
</property>
<property name="anotherBeanList">
<list>
<ref bean="anotherBean1"/>
<ref bean="anotherBean2"/>
</list>
</property>

<!-- set -->
<property name="stringSet">
<list>
<value>ccccc</value>
<value>ddddd</value>
</list>
</property>
<property name="anotherBeanSet">
<list>
<ref bean="anotherBean3"/>
<ref bean="anotherBean4"/>
</list>
</property>

<!-- map -->
<property name="stringMap">
<map>
<entry key="eeeee" value="fffff"/>
<entry key="ggggg" value="hhhhh"/>
</map>
</property>
<property name="anotherBeanMap">
<map>
<entry key-ref="anotherBean5" value-ref="anotherBean6"/>
<entry key-ref="anotherBean7" value-ref="anotherBean8"/>
</map>
</property>

<!-- priperties -->
<property name="properties">
<map>
<entry key="hhhhh" value="iiiii"/>
<entry key="jjjjj" value="kkkkk"/>
</map>
</property>
</bean>

<!-- null -->
<bean id="diByNull" class="com.***.DIByNull">
<property name="anotherBean">
<null/>
</property>
</bean>
<!-- InnerClass -->
<bean id="diByInnerClass" class="com.***.DIByInnerClass">
<property name="anotherBean">
<!-- 内部创建一个Bean,就不需要外部创建Bean再通过id来获取,在XML结构上可以看作高级语言中的局部变量 -->
<bean class="com.***.AnotherBean"/>
</property>
</bean>
</beans>