12、spring的bean基础(4)
在本文中主要介绍以下几个知识点
- 注入日期到bean属性中(使用CustomDateEditor)
- PropertyPlaceholderConfigurer实例
- bean配置继承
下面进入正题
注入日期到bean属性中
第一步:创建bean
package com.main.autowrite.customDateEditor;
import java.util.Date;
public class ShowDate {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "ShowDate [date=" + date + "]";
}
}
第二步:添加bean配置文件
bean配置文件的配置主要有两种方式
方式一:
<?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-2.5.xsd">
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean id="ShowDate" class="com.main.autowrite.customDateEditor.ShowDate">
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2014-12-31" />
</bean>
</property>
</bean>
</beans>
方式二:
首先创建一个日期属性转换器UtilDatePropertyEditor.java,
package com.main.autowrite.customDateEditor;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format="yyyy-MM-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
Date d = sdf.parse(text);
this.setValue(d);
}catch (ParseException e) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
然后在bean配置文件中调用它
注意:以下的配置是基于spring 4.0版本的,如果使用spring 4.0之前的版本,需要把com.main.autowrite.customDateEditor.UtilDatePropertyEditors声明为一个bean,然后在map中引用该bean即可
<?xml version="1.0" encoding="UTF-8"?>
<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 class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date"
value="com.main.autowrite.customDateEditor.UtilDatePropertyEditor"/>
</map>
</property>
</bean>
<bean id="ShowDate" class="com.main.autowrite.customDateEditor.ShowDate">
<property name="date" value="2015-12-23"/>
</bean>
</beans>
测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/main/autowrite/customDateEditor/bean.xml");
ShowDate showDate = (ShowDate) context.getBean("ShowDate");
System.out.println(showDate);
}
PropertyPlaceholderConfigurer实例
在有数据库连接的项目中,一般习惯把数据库连接的详细信息,独立放在一个文件中(以database.properties为例),以便于数据库管理员进行操作。
例子:
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo
jdbc.username=root
jdbc.password=123456
引用database.properties的bean配置文件
<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.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="HelloDAO" class="...">
<property name="dataSource" ref="dataSource" />
</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>
</beans>
详细说明(三步走):
一、导入database.properties文件
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
二、引用database.properties文件的详细信息,并声明为一个名为dataSource的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>
三、引用dataSource bean
<bean id="HelloDAO" class="...">
<property name="dataSource" ref="dataSource" />
</bean>
bean配置继承
1、普通继承
一个实体类Hello.java
Public class Hello{
private int type;
private String name;
//....
}
bean配置文件
<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="Hello" class="com.main.Hello">
<property name="type" value="1" />
</bean>
<bean id="HelloChildren" parent="BaseCustomerMalaysia">
<property name="name" value="jack" />
</bean>
</beans>
输出HelloChildren bean的属性值
HelloChildren [type=1,name=jack]
2、抽象继承
说明:不允许父类bean被实例化,,在上面的普通继承中可以看到,Hello bean依然可以被实例化。
Hello hello = (Hello)context.getBean("Hello");
当我们这样配置时,Hello bean将不能被实例化
<bean id="Hello" class="com.main.Hello" abstract="true">
<property name="type" value="1" />
</bean>
3、纯模版继承
只共享已经设定好的属性值,而不定义或者修改该bean的属性值
4、覆盖父亲bean的属性值
<bean id="Hello" class="com.main.Hello">
<property name="type" value="1" />
</bean>
<bean id="HelloChildren" parent="BaseCustomerMalaysia">
<property name="type" value="" />
<property name="name" value="jack" />
</bean>
这时获取HelloChildren bean输出的结果是:
Hello [type=2,name=jack]

本文详细介绍Spring框架中Bean的配置方法,包括如何注入日期到Bean属性、使用PropertyPlaceholderConfigurer配置外部属性文件以及Bean配置的继承机制。

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



