您的位置:首页 > 编程语言 > Java开发

深入解读Spring Framework IoC容器(第三弹:依赖注入配置详解)

2017-01-05 00:05 666 查看
本篇我们介绍下常用的依赖注入配置。

直接变量

<property/>
元素的value值可以通过字符串形式来指定属性和构造器参数。

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="masterkaoli"/>
</bean>


idref元素

idref元素用来传递容器内其他bean的id值,例如:

<bean id="theTargetBean" class="..."/>

<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>


这段配置就相当于

<bean id="theTargetBean" class="..." />

<bean id="theClientBean" class="...">
<property name="targetName" value="theTargetBean" />
</bean>


区别在于使用idref标签容器会在部署时验证引用的bean是否存在。这个标签可以在我们要使用其他bean的id值的时候防止我们拼错。

ref元素

ref元素用来将bean中指定属性的值设置为对容器的另外一个bean的引用。 该引用bean将被作为依赖注入。例如:

<ref bean="someBean"/>


内部bean

内部bean(inner bean)是指在
<property/>
或者
<constructor-arg/>
元素内部使用
<bean/>
定义bean。内部bean没有id、name属性以及scope。内部bean总是匿名的并且他们总是伴随着外部bean创建。内部bean也不能被注入到其他的bean。

<bean id="outer" class="...">
<property name="target">
<!-- 这就是内部bean -->
<bean class="com.example.Person">
<property name="name" value="Fiona Apple"/>
<property name="age" value="25"/>
</bean>
</property>
</bean>


集合

集合元素有
<list/>
<set/>
<map/>
,和
<props/>
,写法如下:

<bean id="moreComplexObject" class="example.ComplexObject">
<!-- props对应java.util.Properties -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- list对应java.util.List -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- map对应java.util.Map -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- set对应java.util.Set -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>


集合合并

<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- 两种不同类型的集合是不能合并的 -->
<!-- 当子bean被容器初始化,其adminEmails将与父bean的adminEmails属性进行合并。 -->
<!-- 子bean的集合从父bean集成所有属性元素,同时子bean的相同key的prop值将覆盖父集合的相应值。 -->
<!-- merge属性必须在继承的子bean中定义 -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>


对于list要注意,父bean的list内容将排在子bean的list内容的前面。

强类型集合转换

public class Foo {

private Map<String, Float> accounts;

public void setAccounts(Map<String, Float> accounts) {
this.accounts = accounts;
}
}


<beans>
<bean id="foo" class="x.y.Foo">
<!-- 这个属性准备注入时,通过反射获得代码中定义强类型Map<String, Float>,可以自动转换 -->
<property name="accounts">
<map>
<entry key="one" value="9.99"/>
<entry key="two" value="2.75"/>
<entry key="six" value="3.99"/>
</map>
</property>
</bean>
</beans>


Null和空字符串

<bean class="ExampleBean">
<property name="email" value=""/>
</bean>


<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐