1、PropertyPlaceholderConfigurer实例
在实际的项目应用中,常常将数据库的链接配置信息保存在.properties文件中,将数据库访问驱动保存到spring配置文件中,为自动从属性文件中读取保存的数据库配置信息,使用 PropertyPlaceholderConfigurer 类在spring配置文件中设置与数据源配置相对应的属性文件
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--定义数据配置属性文件为jdbc.properties-->
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!--${jdbc.driverClassName} 对应属性文件的属性名称为jdbc.driverClassName--> <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>类似诸如日志文件实例需要从配置文件读取时,也可以采用类似方式
2、Bean的继承
bean支持继承,甚至可以将父bean定义为抽象类,该抽象bean不允许采用getBean方式实例化,子bean继承父bean时,继承父bean对象的属性,当然也可在子bean中重新定义父bean中指定的属性值,对继承属性进行覆盖。bean的继承相应的spring信息配置如下示意:
<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="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean>
<bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
</beans>
3、依赖检查
spring可设置对属性的赋值情况进行检查,支持四种级别额属性赋值检查
- none – 没有依赖检查,这是默认的模式。
- simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
- objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
- all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。
对bean没有明确指定属性检查方式即为:none方式;
<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="CustomerBean" class="com.yiibai.common.Customer"
dependency-check="simple">
<property name="person" ref="PersonBean" />
<property name="action" value="buy" />
</bean>
<bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
</beans>
以上设置bean依赖检查方式为simple;设定至少检查一项属性。也可采用在java代码中设置属性为必须项的方式,进行属性依赖检查,示例代码如下:
public class Customer
{
private Person person;
private int type;
private String action;
public Person getPerson() {
return person;
}
@Required
public void setPerson(Person person) {
this.person = person;
}
}
简单地套用@Required注解不会强制执行该属性的检查,还需要注册一个RequiredAnnotationBeanPostProcessor以了解在bean配置文件@Required注解。spring配置文件如下示例:
<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">
<!--在spring中引入context:annotation-config配置(方法一)-->
<context:annotation-config />
<!--在spring中引入RequiredAnnotationBeanPostProcessor(方法二)--> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.yiibai.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.yiibai.common.Person"> <property name="name" value="yiibai" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean></beans> 当person属性未设置时,触发依赖检查异常
4、实现InitializingBean和DisposableBean接口
-
实现 InitializingBean,在 属性设置之后执行afterPropertiesSet()方法。
-
实现DisposableBean,在 Spring 容器释放该 bean 之后运行 destroy()。即在调用 context.close()之后执行destroy方法
<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.yiibai.customer.services.CustomerService"
init-method="initIt" destroy-method="cleanUp">
<property name="message" value="i'm property message" />
</bean>