javascript
【Spring 基础篇三】属性注入与属性编辑器
? ? ?上篇我們了解了一下applicationContext.xml的兩種注入方式,本篇我們來了解一下關于屬性的注入以及操作。
? ? ?在敲代碼的過程中,我們很容易遇到這樣的問題,比如一個List的集合,我之前給他賦值兩個,現(xiàn)在我想給他再重新賦值,一般的想法當然是修改代碼,然后完成自己想要的需求了,但是這種做法是不可取了,所以我們完成可以采用Spring屬性注入的方式來解決此問題??慈缦耫emo:
1、我們創(chuàng)建一個類,來創(chuàng)建幾個不同類型的屬性:
常用屬性的注入有:int,String,list,set,map等。
Bean01屬性類:
public class Bean01 {private String strValue; //String類型實體private int intValue; //int類型實體<span style="font-family: Arial, Helvetica, sans-serif;"> </span>private List listValue; //list類型實體private Set setValue; //Set類型實體private String[] arrayValue; //String集合實體private Map mapValue; //Map集合實體private Date dateValue; //date類型實體…………省略get,set方法 }? ? ?由于有了set方法,所以我們完全可以采用setter注入的方式對其屬性賦值applicationContext.xml具體屬性賦值如下:
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="bean01" class="com.bjpowernode.spring.Bean01"><property name="strValue" value="Hello_Spring" /><property name="intValue" value="123" /><property name="listValue"><list><value>list1</value><value>list2</value></list></property><property name="setValue"><set><value>set1</value><value>set2</value></set></property><property name="arrayValue"><set><value>array1</value><value>array2</value></set></property><property name="mapValue"><map><entry key="k1" value="v1"></entry><entry key="k2" value="v2"></entry></map></property><property name="dateValue" value="2011-10-21"> //日期格式賦值</property></bean> </beans>? ? ?但是有好多信息并非是簡簡單單的賦值就能夠滿足需求的,比如我們的日期,存有各種格式,到界面會存在亂碼或者格式顯示的問題,相關日期亂碼博客Laydate日期亂碼。我們該如何去應如自如呢?當然,對于這種情況,我們完全可以單獨羅列出來,新建一個類來完成我們的需求,對于這個類的作用,我們稱其為“屬性編輯器”。
如何自定義屬性編輯器?- 繼承PropertyEditorSupport
- 覆蓋setAsText()方法
- 將自定義的屬性編輯器注入到spring中
創(chuàng)建UtilDatePropertyEditor類:
// 繼承于Java beans的PropertyEditorSupport來實現(xiàn)我們的功能。 public class UtilDatePropertyEditor extends PropertyEditorSupport {// 定義一個實體,獲得set方法private String pattern;// 獲得的set方法,便于注入public void setPattern(String pattern) {this.pattern = pattern;}// 覆蓋方法setAsText滿足需求@Overridepublic void setAsText(String text) throws IllegalArgumentException {// 輸出來,便于測試顯示結(jié)果System.out.println("---UtilDatePropertyEditor.setAsText()---" + text);try {// 只適用于一種格式Date date = new SimpleDateFormat(pattern).parse(text);// 將其賦值進去this.setValue(date);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();// 異常拋出throw new IllegalArgumentException(text);}} }? ? ?由于Spring是支持多文件配置的,就和文件一樣,每個文件都干著不一樣的事,所以我們可以再新建一個配置applicationContext-editor.xml,用來專門為屬性編輯器服務。 <?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 注:ID不能和之前的配置文件重復 --><!-- 將屬性編輯器注入給Spring,Class為CustomEditorConfigurer的路徑 --><bean id="customEditors"class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!--將屬性編輯器放進CustomEditorConfigurer的成員變量customEditors中進行保存,通過setter方式注入 --><property name="customEditors"><map><!--key值為要轉(zhuǎn)換的類型 --><entry key="java.util.Date"><!--內(nèi)部轉(zhuǎn)換器 --><bean class="com.bjpowernode.spring.UtilDatePropertyEditor"><!--通過setter的方式注入對pattern(多個日期的選擇)的設置 --><property name="pattern" value="yyyy-MM-dd"></property></bean></entry></map></property></bean><!--轉(zhuǎn)換器 --><!--注:這個轉(zhuǎn)換器到底是私有還是共有,放在外邊則是大家都可以使用,我們可以選擇放在里邊 --><!-- <bean id="utilDatePropertyEditor" class="com.bjpowernode.spring.UtilDatePropertyEditor"></bean> --> </beans>? ? ?這樣我們的關于日期的一個屬性編輯器就完工了,不管是pattern,日期類型的注入還是整個編輯器的注入,整個過程都采用的setter的注入方式,最后我們使用單元測試的方式測試一下: public class InjectionTest extends TestCase {// 定義成員變量private BeanFactory factory;@Overrideprotected void setUp() throws Exception {// 只創(chuàng)建一次對配置文件的讀取/** factory = new* ClassPathXmlApplicationContext("applicationContext.xml");*/// 傳遞數(shù)組,兩個配置文件的讀取String[] configLocations = new String[] { "applicationContext.xml","applicationContext-editor.xml" };factory = new ClassPathXmlApplicationContext(configLocations);}@Overrideprotected void tearDown() throws Exception {// 銷毀,刪除的方法}public void testInjection1() {Bean01 bean01 = (Bean01) factory.getBean("bean01");System.out.println("bean01.strValue=" + bean01.getStrValue());System.out.println("bean01.intValue=" + bean01.getIntValue());System.out.println("bean01.listValue=" + bean01.getListValue());System.out.println("bean01.setValue=" + bean01.getSetValue());System.out.println("bean01.arrayValue=" + bean01.getArrayValue());System.out.println("bean01.mapValue=" + bean01.getMapValue());System.out.println("bean01.dateValue=" + bean01.getDateValue());}最后的結(jié)果:總結(jié):
? ? ?1、通過spring的BeanFactory實例化,配置和管理對象,當有多個對象的時候,其實除了數(shù)組的方式來管理,我們還可以采取規(guī)范化的管理方式,在配置文件定義名字的時候,我們可以規(guī)定為統(tǒng)一的格式,比如上述例子的兩個文件,我們可以修改名稱為:“applicationContext-beans.xml”、“applicationContext-editor.xml”
這樣我們則可以簡單化調(diào)用:
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");轉(zhuǎn)載于:https://www.cnblogs.com/huohuoL/p/10545496.html
總結(jié)
以上是生活随笔為你收集整理的【Spring 基础篇三】属性注入与属性编辑器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 仿照微信的效果,实现了一个支持多选、选原
- 下一篇: 老李分享:Android -自动化埋点