zoukankan      html  css  js  c++  java
  • Spring>属性的注入

    一、普通属性的注入

    <bean id="bean1" class="com.bjsxt.spring.Bean1">
      <property name="strValue" value="Hello"/>
      <!-- 
      <property name="intValue" value="123"/>
       -->
       <property name="intValue">
        <value>123</value>
       </property>
       <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">
        <list>
         <value>array1</value>
         <value>array2</value>
        </list>
       </property>
       <property name="mapValue">
        <map>
         <entry key="k1" value="v1"/>
         <entry key="k2" value="v2"/>
        </map>
       </property>
     </bean>

    二、自定义属性编辑器

    1、什么是属性编辑器,作用?
    自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入。
    spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器。

    2、 如何定义属性编辑器?
    继承PropertyEditorSupport类,覆写setAsText()方法。

    例子:自定义属性编辑器,将日期字符串转换成java.util.Date对象,注入到bean中。

    	<bean id="SimplePropertyBean" class="com.ncepu.spring.SimplePropertyBean">
    		<property name="date">
    			<value>2008-12-14</value>
    		</property>
    	</bean>
    

    UtilDatePropertyEditor.java

    package com.ncepu.spring;
    
    import java.beans.PropertyEditorSupport;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.omg.CORBA.PRIVATE_MEMBER;
    
    public class UtilDatePropertyEditor extends PropertyEditorSupport {
    
     private String format;
    	@Override
    	public void setAsText(String text) throws IllegalArgumentException {
    		
    		SimpleDateFormat sdf=new SimpleDateFormat(format);
    		try {
    			Date date=sdf.parse(text);
    			this.setValue(date);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		
    	}
    
    	public void setFormat(String format) {
    		this.format = format;
    	}
    
    }
    

    将属性编辑器注册到spring配置文件中

    <bean id="customEditorConfigurer"
    		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    		<property name="customEditors">
    			<map>
    				<entry key="java.util.Date">
    					<bean class="com.ncepu.spring.UtilDatePropertyEditor">
    						<property name="format" value="yyyy-MM-dd" />
    					</bean>
    				</entry>
    			</map>
    		</property>
    
    	</bean>

    三、公共属性的注入

    1、解决配置量大的问题

    分文件factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");

    将公共的注入提取出来
     --通过<bean>标签定义公共的属性,指定abstract=true
     --具有相同属性的类在<bean>标签中指定其parent属性

    比如bean2中有id,name,password,email四个属性,bean3中有id,name,password,telephone四个属性,它们有公共属性id,name,password。

    <bean id="beanAbstract" abstract="true">
    		<property name="id" value="1112227142"></property>
    		<property name="name" value="ceit"></property>
    		<property name="password" value="ceit"></property>
    	</bean>
    
    	<bean id="bean2" class="com.ncepu.spring.Bean2" parent="beanAbstract">
    		<property name="email" value="492230553@qq.com"></property>
    	</bean>
    
    	<bean id="bean3" class="com.ncepu.spring.Bean3" parent="beanAbstract">
    		<property name="telephone" value="18810535304"></property>
    	</bean>
    
    


        
      
       

  • 相关阅读:
    【CodeForces 611C】New Year and Domino
    【HDU 1003】 Max Sum
    【ZOJ 3502】Contest
    【LightOJ 1422】Halloween Costumes(区间DP)
    【ZOJ 3609】Modular Inverse
    乘法逆元及其应用
    除法和取余的运算时间
    HUD3689 Infinite monkey theorem
    POJ2185 Milking Grid
    1355: [Baltic2009]Radio Transmission[循环节]
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637179.html
Copyright © 2011-2022 走看看