`

[SXT][WY]Spring04 属性注入

阅读更多

普通属性注入

 

依赖对象的注入方式,可以采用:
 * ref属性
 * <ref>标签
 * 内部<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: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.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 
 <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>
   <property name="dateValue">
    <value>2008-08-15</value>
   </property>
 </bean>
 
 </beans>

 

public class Bean1 {
 
 private String strValue;
 
 private int intValue;
 
 private List listValue;
 
 private Set setValue;
 
 private String[] arrayValue;
 
 private Map mapValue;
 
 private Date dateValue; // 字符串转换成date类需要一个特定的转换器。下一个段落中介绍

...

}

 

public class InjectionTest extends TestCase {
 
 private BeanFactory factory;
 
 @Override // testcase 中的初始化方法
 protected void setUp() throws Exception {
  factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); 
 }

 public void testInjection1() {
  Bean1 bean1 = (Bean1)factory.getBean("bean1");
  
  System.out.println("bean1.strValue=" + bean1.getStrValue());
  System.out.println("bean1.intValue=" + bean1.getIntValue());
  System.out.println("bean1.listValue=" + bean1.getListValue());
  System.out.println("bean1.setValue=" + bean1.getSetValue());
  System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
  System.out.println("bean1.mapValue=" + bean1.getMapValue());
  System.out.println("bean1.dateValue=" + bean1.getDateValue());
 }

}

 

自定义属性编辑器

 

 上面段落中的

   <property name="dateValue">
    <value>2008-08-15</value>
   </property>

中的字符串要转换成date类型,需要转换器的支持。

 

什么是属性编辑器,作用?
 * 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
 spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
 
 * 如何定义属性编辑器?
  * 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
  * 将属性编辑器注册到spring中,参见:applicationContext-editor.xml

 

UtilDatePropertyEditor.java

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * java.util.Date属性编辑器
 * @author Administrator
 *
 */
public class UtilDatePropertyEditor extends PropertyEditorSupport {

 private String format="yyyy-MM-dd";
 
 @Override
 public void setAsText(String text) throws IllegalArgumentException {
  System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);
  
  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;
 }

}

 

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.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <!-- 定义属性编辑器 -->     
 <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
   <map>
    <entry key="java.util.Date">
     <bean class="com.bjsxt.spring.UtilDatePropertyEditor">
      <property name="format" value="yyyy-MM-dd"/>
     </bean>
    </entry>
   </map>
  </property>
 </bean> 
 
 <!--
 <bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean>
  -->
</beans>

 

 

 

公共属性的注入

 

当几个bean有相同的属性结构时,可以缩减配置,抽取公共属性设置

 

下面bean3 bean4有公共的属性

 <bean id="bean2" class="com.bjsxt.spring.Bean2">
  <property name="bean3" ref="bean3"/>
  <property name="bean4">
   <ref bean="bean4"/>
  </property> 
  <property name="bean5" ref="bean5"/>
 </bean>
 
 <!--
 <bean id="bean3" class="com.bjsxt.spring.Bean3">
  <property name="id" value="1000"/>
  <property name="name">
   <value>Jack</value>
  </property>
  <property name="password" value="123"/>
 </bean>
 
 <bean id="bean4" class="com.bjsxt.spring.Bean4">
  <property name="id" value="1000"/>
  <property name="name" value="Jack"/>
 </bean>
  -->
 
 <bean id="bean5" class="com.bjsxt.spring.Bean5">
  <property name="age" value="20"/>
 </bean>

 

可以抽取相同属性:

在另一个单独的配置文件中配置

applicationContext-other.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.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  
   <bean id="beanAbstract" abstract="true">
     <property name="id" value="1000"/>
     <property name="name" value="Jack"/>
   </bean>        
  
   <bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
     <property name="name" value="Tom"/>
     <property name="password" value="123"/>
   </bean>       
  
   <bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>

 

如何将公共的注入定义描述出来?
 * 通过<bean>标签定义公共的属性,指定abstract=true
 * 具有相同属性的类在<bean>标签中指定其parent属性
 
 参见:applicationContext-other.xml

分享到:
评论

相关推荐

    SXT--RBAC权限控制系统源码

    SXT--RBAC权限控制系统源码

    mikrotik SXT 网桥点对点安装

    mikrotik SXT 网桥点对点安装 全过程配置截图,拓扑图

    11spring4_aop3.rar

    &lt;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" xsi:schemaLocation=...

    05spring4_di.rar

    -- p命名空间注入属性依然要设置set方法 --&gt; &lt;bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"/&gt; &lt;!--c命名空间注入要求有对应参数的构造方法 --&gt; &lt;bean id="u1" class="cn.sxt.vo.User...

    sxt.rar_sxt

    用VC++编写的一个开启摄像头的程序,可以不用安装驱动。

    sxt Video File

    Video 开发 文档 所有下载文档 比较全,开发方便用

    SXT shell_SXTshell_

    sxt shell 自动安装shell 脚本

    sxt\weblogic\weblogic安装.avi

    sxt\weblogic\weblogic安装.avi sxt\weblogic\weblogic安装.avisxt\weblogic\weblogic安装.avi sxt\weblogic\weblogic安装.avi

    sxt66329PPT模板.pptx

    sxt66329PPT模板.pptx

    sxt_api_14.jar

    sxt_api_14.jar,便于下载可以使用

    08spring4_dynamicproxy.rar

    package cn.sxt.dynamicproxy; import java.util.ArrayList; import java.util.List; import cn.sxt.service.UserService; import cn.sxt.service.UserServiceImpl; public class Client { public ...

    C#查看图片缩略图源码-8Sxt.rar

    C#查看图片缩略图源码-8Sxt.rar

    SXT_ksxt_

    加热后软件恶霸vi热v贵部 将二姑vUI热 进欧冠ire

    sxt.rar_sxt_手写_手写 识别_手写体 识别_文字识别

    一个手写体文字识别的C++源代码,新思想新方法。

    SXT-FastDFS.pdf

    FastDFS 为互联网量身定制, 充分考虑了冗余备份、 负载均衡、 线性扩容等机制, 并注 重高可用、 高性能等指标, 使用 FastDFS 很容易搭建一套高性能的文件服务器集群提供文件 上传、 下载等服务。...

    sxt.rar_数据结构

    链表算法程序实现,指针操作干净利索,很经典的算法程序,

    03spring4_ioc2.rar

    package cn.sxt.dao; public interface UserDao{ public void getUser(); }

    sxt+doswin1.zip

    我把这两个软件放在一起了,刘经理推荐的,一个是摄像头拍照片一个是制作dos gui的代码,请苗工程师使用看看。

    sxt.zip_VBa_摄像头

    万能摄像头 摄像头抓拍,获取摄像头图片 万能摄像头 摄像头抓拍,获取摄像头图片 万能摄像头 摄像头抓拍,获取摄像头图片

    sxt.rar_c# 摄像头实例

    用C#编写的摄像头控制软件,一个简单的实例。

Global site tag (gtag.js) - Google Analytics