在本文中主要介紹以下幾個知識點
注入日期到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]新聞熱點
疑難解答