<?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-3.0.xsd">
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<import resource="../resources/configFile.xml"/>
<import resource="classpath:../configFile.xml"/>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
2. 获取Container容器的实例
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
//or
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
//or
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");
3. bean 实例化
1> 工厂静态方法实例化:
配置:
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>类:
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private DefaultServiceLocator() {}
public ClientService createClientServiceInstance() {
return clientService;
}
}
2> 构造函数实例化:
1) 依赖关系:
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
}
}<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
2)构造函数赋值:
package examples;
public class ExampleBean {
private int years;
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}按类型
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
按序列
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>按名称
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>4 属性赋值:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<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>或:<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="masterkaoli"/>或:<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>集合类型的属性赋值:
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<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>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>The value of a map key or value, or a set value, can also again be any of the following elements:
bean | ref | idref | list | set | map | props | value | null
集合类型的属性赋值及合并继承
<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">
<!-- the merge is specified on the *child* collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>child.getAdminEmails()结果为
=========================================
administrator=administrator@example.com
sales=sales@example.com
support=support@example.co.uk
=========================================
Null值和空值(Null and empty string values)
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>等同于 exampleBean.setEmail("");<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>等同于exampleBean.setEmail(null).属性赋值的简写形式: p:namespace
<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-3.0.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="foo@bar.com"/>
<property name="user" ref="aUser"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean" p:email="foo@bar.com"/>
<bean name="p-namespace-ref" class="com.example.ExampleBean" p:email="foo@bar.com" p:user-ref="aUser"/>
<bean name="aUser" class="com.example.User">
<property name="userName" value="tiger"/>
<property name="age" value="24"/>
</bean>
</beans>构造函数的简写形式: c:namespace
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="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="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
<-- 'traditional' declaration -->
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
<constructor-arg value="foo@bar.com"/>
</bean>
<-- 'c-namespace' declaration -->
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com">
</beans>
5 依赖
有多个依赖时可用逗号,空格,分号分割
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<!--在beanOne实例化或消亡之前会先实例化或消亡manager和accountDao-->
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />有依赖关系的bean,不但在实例化时存在依赖关系,在对象消亡时也同样具有依赖关系。即在实例化或消亡时会先实例化或消亡被依赖对象。6.Lazy-initialized 延时实例化
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
延时实例化的bean,会在被访问的时候才进行实例化,而不是在容器启动是进行pre-instantiation
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext
creates the lazy-initialized bean at startup, because it must satisfy the singleton's dependencies.
The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.
当一个singleton 的bean依赖于Lazy-initialized的bean时,Lazy-initialized将会被实例化。
Lazy-initialized同样可以作用于容器级别:
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>

本文深入解析Spring框架中Bean实例化的方法,包括工厂静态方法实例化、构造函数实例化、属性赋值及集合类型的属性赋值,并详细讲解了依赖注入的概念与实践。同时,阐述了如何实现延迟实例化以及懒加载机制的应用。
1314

被折叠的 条评论
为什么被折叠?



