国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

12、spring的bean基礎(4)

2019-11-08 02:23:22
字體:
來源:轉載
供稿:網友

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]
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁明县| 鄂州市| 南宫市| 沙洋县| 蕲春县| 岢岚县| 麦盖提县| 肇州县| 全州县| 木兰县| 清镇市| 台江县| 正定县| 壶关县| 成安县| 临武县| 陇川县| 肇庆市| 宣化县| 巫溪县| 胶州市| 泰兴市| 石泉县| 江安县| 天祝| 依兰县| 石楼县| 庆元县| 仙居县| 长岭县| 蒲江县| 锡林郭勒盟| 江达县| 罗江县| 锡林浩特市| 新泰市| 随州市| 德钦县| 肇庆市| 大新县| 自治县|