1.引用Bean
<ref bean="someBean"/>
如:
<bean id="A" class="com.B">
<property name="C" >
<ref bean="C1"/>
</property>
</bean>
2.需指定必定是同个XML下的Bean时,用
<ref local="someBean"/>
3.属性值的注入
<bean id="AAA" class="com.BB">
<property name="name">
<value>XXX</value>
</property>
<property name="type">
<value>txt</value>
</property>
</bean>
//或者
<bean id="AAA" class="com.BB">
<property name="name" value="XXX" />
<property name="type" value="txt" />
</bean>
使用P模型
xmlns:p="http://www.springframework.org/schema/p"
<bean id="AAA" class="com.BB" p:name="XXX" p:type="txt" />
这样更加简便
4.引入多个XML文件
通过包含关系
<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-2.5.xsd">
<import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-ModuleA.xml"/>
</beans>
指定目录,参考是class根目录
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml",
"Spring-Connection.xml","Spring-ModuleA.xml"});
5.内嵌Bean时
<bean id="XXX" class="com.AAA">
<property name="person">
<bean class="com.BB">
<property name="name" value="YY" />
<property name="age" value="22" />
</bean>
</property>
</bean>
指的是在AAA类中,有个person属性的Persion类,我们使用set的方式注入一个新建的Persion的Bean对象,我们也可以用构造器的方式注入
<bean id="XXX" class="com.AAA">
<constructor-arg>
<bean class="com.Person">
<property name="name" value="mkyong" />
<property name="age" value="28" />
</bean>
</constructor-arg>
</bean>
注意的是不要在Person的单独bean对象中注入persion的属性,而是通过上面的方式创建一个临时bean注入XXXbean中
6.bean的scope设置
5种配置
singleton 始终返回一个单一的Bean对象
prototype 每次都返回一个新的bean对象
request 通过HTTP request.请求返回单一的bean
session 通过HTTP session.请求返回单一的bean
globalSession 通过全局HTTP session.请求返回单一的bean
3.0时默认配置是singleton
设置时只要在bean的属性里设置,例如设置prototype
scope="prototype"
或者通过Anno注解
@Scope("prototype")
7.集合
主要是这几种,举例
这里是泛泰指定的时候,如List<Object> list属性
List – <list/>
Set – <set/>
Map – <map/>
Properties – <props/>
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.Person">
<property name="name" value="mkyongList" />
<property name="age" value="33" />
</bean>
</list>
</property>
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.Person">
<property name="name" value="mkyongSet" />
<property name="age" value="28" />
</bean>
</set>
</property>
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.Person">
<property name="name" value="mkyongMap" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property
<property name="pros">
<props>
<prop key="admin">admin@nospam.com</prop>
<prop key="support">support@nospam.com</prop>
</props>
</property>>
8.集合(工厂模式)
上面我们是指定List等的类型类的类型,如果不制定的话,让我们装配一个全新的集合
如
private List lists;
<bean id="CustomerBean" class="com.Customer">
<property name="lists">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</property>
</bean>
我们通过
targetListClass指定list的类型类的类型
让后开始填充值
或者通过
xmlns:util=<a target=_blank href="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a>
便签
<bean id="CustomerBean" class="com.Customer">
<property name="lists">
<util:list list-class="java.util.ArrayList">
<value>1</value>
<value>2</value>
<value>3</value>
</util:list>
</property>
</bean>
还有其他集合
Set
<bean id="CustomerBean" class="com.Customer">
<property name="sets">
<bean class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="targetSetClass">
<value>java.util.HashSet</value>
</property>
<property name="sourceSet">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</property>
</bean>
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="sets">
<util:set set-class="java.util.HashSet">
<value>1</value>
<value>2</value>
<value>3</value>
</util:set>
</property>
</bean>
Map
<bean id="CustomerBean" class="com.Customer">
<property name="maps">
<bean class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="targetMapClass">
<value>java.util.HashMap</value>
</property>
<property name="sourceMap">
<map>
<entry key="Key1" value="1" />
<entry key="Key2" value="2" />
<entry key="Key3" value="3" />
</map>
</property>
</bean>
</property>
</bean>
<bean id="CustomerBean" class="com.Customer">
<property name="maps">
<util:map map-class="java.util.HashMap">
<entry key="Key1" value="1" />
<entry key="Key2" value="2" />
<entry key="Key3" value="3" />
</util:map>
</property>
</bean>
9.特殊的注入Data的bean
通过两种方式
"工厂模式"
Date date;
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean id="customer" class="com.mkyong.common.Customer">
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2010-01-31" />
</bean>
</property>
</bean>
通过
CustomDateEditor
<bean id="dateEditor"
class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg>
<bean class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
</constructor-arg>
<constructor-arg value="true" />
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor" />
</entry>
</map>
</property>
</bean>
<bean id="customer" class="com.mkyong.common.Customer">
<property name="date" value="2010-02-31" />
</bean>
10.通过占位符配置一些属性
常用于数据库等常修改属性的Bean
新建properties文件
如配置数据的属性(都是放在class的根目录里的)
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mkyongjava
jdbc.username=root
jdbc.password=password
引入
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
使用
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
11.继承性,就是利用之前配置的Bean再进行配置
<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer">
<property name="country" value="Malaysia" />
</bean>
<bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
如果禁止继承,可以在bean进行限制abstract="true"
<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean>
12.注入的检测
有时我们要让一个bean的属性全部被注入才可以使用,默认的情况下时none
有这些检测规则
none – No dependency checking.
simple – If any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, UnsatisfiedDependencyException will be thrown.
objects – If any properties of object type have not been set, UnsatisfiedDependencyException will be thrown.
all – If any properties of any type have not been set, an UnsatisfiedDependencyException will be thrown.
public class Customer
{
private Person person;
private int type;
private String action;
//getter and setter methods
}
<bean id="CustomerBean" class="com.mkyong.common.Customer"
dependency-check="simple">
<property name="person" ref="PersonBean" />
<property name="action" value="buy" />
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
由于Customer Bean的type属性没有装配所以会失败
而objects规则是检验Object(非简单类型)对象是否被配置
<bean id="CustomerBean" class="com.mkyong.common.Customer"
dependency-check="objects">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
没有装配private Person person;同样会出错
为了简便你也可以全局配置
举例配置default-dependency-check="all"规则
<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-2.5.xsd"
default-dependency-check="all">
13.对于上面的检测规则是应用与整个对象的,如果我们需要对某个属性进行Check
@Required
public void setPerson(Person person) {
this.person = person;
}
这需要注册RequiredAnnotationBeanPostProcessor
这样就饿可以
RequiredAnnotationBeanPostProcessor <beans
...
xmlns:context="http://www.springframework.org/schema/context"
...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
...
<context:annotation-config />
...
</beans>
14.设置bean的init-method and destroy-method回调
<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-2.5.xsd">
<bean id="customerService" class="com.mkyong.customer.services.CustomerService"
init-method="initIt" destroy-method="cleanUp">
<property name="message" value="i'm property message" />
</bean>
</beans>
public class CustomerService
{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
}
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}
}
或者注解的方式
@PostConstruct
public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
}
@PreDestroy
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}
By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘<context:annotation-config />‘ in bean configuration file
1. CommonAnnotationBeanPostProcessor
<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-2.5.xsd">
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean>
</beans>
2. <context:annotation-config />
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean>
</beans>