学习spring-core-2 days
1.4.2 依赖关系和配置
Spring的基于XML的配置元数据为此目的支持其元素 和 元素中的子元素类型。
<!-- 写法1 -->
<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="root" />
</bean>
简洁的XML配置:
<!-- 写法2 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="root"
/>
</bean>
</beans>
配置java.util.Properties
:
<!-- 写法3 -->
<bean id="properties" class="prg.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>
idref
元素的用法:
idref
元素只是一种防错方法,可以将id容器中另一个bean的字符串值(而不是引用)传递给一个或元素。
<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>
ref 其他bean 类
如何使用parent
属性
<!-- 定义父bean内容 -->
<bean id="accountService" class="com.something.SimpleAccountService">
...
</bean>
<!-- 在子bean内容 -->
<!-- bean 名称与父 bean 相同, 代理工厂bean -->
<bean id="accountService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/>
</property>
...
</bean>
4.0 框架后,将现有
ref local
应用更改ref bean
内部 bean
元件 <property/>
或 <constructor-arg/>
示例:
<bean id="outer" class="...">
<property name="target">
<!-- 这是一个内部bean -->
<bean class="com.example.Person">
<property name="name" value="apple"/>
<property name="age" value="18"/>
</bean>
</property>
</bean>
集合
<list/>
<set/>
<map/>
<props/>
示例
<bean id="moreComplexObject" class="exmaple.ComplexObject">
<!-- 结果中设置一个管理邮件的调用 -->
<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>
<!-- 结果中在一个集合列表调用 -->
<property name="someList">
<list>
<value>列表元素, 后跟引用</value>
<ref bean="myDataSource"/>
</list>
</property>
<!-- 结果中集合map调用 -->
<property name="someMap">
<map>
<entry key="entry" value="just some string" />
<entry key="ref" value-ref="myDataSource" />
</map>
</property>
<!-- 结果中集合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">
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
</beans>
java5中引入泛型类型
public class SomeClass{
private Map<String, Float> accounts;
public void setAccounts(Map<String, Float> accounts){
this.accounts = accounts;
}
}
<beans>
<bean>
<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>
空字符串值
Spring将属性等的空参数视为空Strings
<bean class="ExampleBean">
<property name="email" value="" />
</bean>
等效Java代码
exampleBean.setEmail("");
<null/>
元素处理的值。
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
等效Java代码
exampleBean.setEmail(null);
带有p命名空间的XML快捷方式
示例,第一个使用标准XML格式,第二个使用p命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@some.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@some.com"/>
</beans>
示例,两个bean定义,都引用另外一个bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 -->
<bean name="anotherBean" class="com.example.Person">
<property name="name" value="luce"/>
</bean>
<!-- 定义两个bean,然后引入另外一个bean -->
<bean name="john-classic" class="com.example.Person">
<property name="name" value="Doe"/>
<property name="spouse" ref="anotherBean"/>
</bean>
<bean name="john-modern"
class="com.example.Person"
p:name="Doe"
p:spouse-ref="anotherBean"/>
</beans>
带有c命名空间的XML快捷方式
示例,基于构造函数的依赖注入
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="thingTwo" class="x.y.ThingTwo" />
<bean id="thingThree" class="x.y.ThingThree"/>
<!--传统声明-->
<bean id="thingOne" class="x.y.ThingOne">
<constructor-arg ref="thingTwo"/>
<constructor-arg ref="thingThree"/>
<constructor-arg value="something@some.com"/>
</bean>
<!-- c-namespace 声明-->
<bean id="thingOne"
class="x.y.ThingOne"
c:thingTwo-ref="thingTwo"
c:thingThree-ref="thingThree"
c:email="something@some.com"/>
</beans>
c-namespace 索引声明 (不建议使用 )
<bean id="thingOne" class="x.y.ThingOne" c:_0-ref="thingTwo" c:_1-ref="thingThree"/>
复合属性名称
<bean id="something" class="things.ThingOne">
<proerty name="fred.bob.sammy" value="123" />
</bean>
1.4.3 使用依赖
示例,使用depends-on
属性表示对单个bean的依赖关系:
<bean id="beanOne" class="ExampleBean" depends-on="manager" />
<bean id="manager" class="ManagerBean" />
示例,多个bean依赖关系,(逗号,空格和分号是有效的分隔符)
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager"/>
...
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
1.4.4 懒惰初始化的bean类
概念:
默认情况下,ApplicationContext
实现会急切地创建和配置所有单例bean作为初始化过程的一部分。通常,这种预先实例化是可取的,因为配置或周围环境中的错误是立即发现的,而不是几个小时甚至几天后。如果不希望出现这种情况,可以通过将bean定义标记为延迟初始化来阻止单例bean的预实例化。
延迟初始化的bean告诉IoC容器在第一次请求时创建bean示例,而不是在启动时。
示例,在XML中,此行为由 元素lazy-init
上的属性控制<bean/>
<bean id="lazy" class="com.something.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.something.AnotherBean"/>
示例,使用元素default-lazy-init
属性来控制容器级别的延迟初始化
<beans default-lazy-init="true">
...
</beans>
1.4.5 自动化协作者
概念:
Spring容器可以自动连接协作bean之间的关系。
自动装配具有优点:
- 自动装配可以显示减少指定属性或构造函数参数的需要。
- 自动装配可以随着对象的发展更新配置。
使用基于XML的配置元数据时,可以使用元素的autowire
属性为bean定义指定autowire模式<bean/>
。
自动装配有四种模式:
模式 |
---|
no |
byName |
byType |
constructor |
no: (默认)