zoukankan      html  css  js  c++  java
  • (原)spring学习笔记4.普通属性注入

    参见spring中文帮助.chm文档
    代码参考whyspringofioc
    1.首先来个Bean1类

     1package com.zyl.spring;
     2
     3import java.util.Date;
     4import java.util.List;
     5import java.util.Map;
     6import java.util.Set;
     7public class Bean1 {
     8   private String strValue;
     9   private int intValue;
    10   private List listValue;
    11   private Set setValue;
    12   private String[] arrayValue; //数组
    13   private Map mapValue;
    14   private Date dateValue;
    15public Date getDateValue() {
    16    return dateValue;
    17}

    18public void setDateValue(Date dateValue) {
    19    this.dateValue = dateValue;
    20}

    21public String getStrValue() {
    22    return strValue;
    23}

    24public void setStrValue(String strValue) {
    25    this.strValue = strValue;
    26}

    27public int getIntValue() {
    28    return intValue;
    29}

    30public void setIntValue(int intValue) {
    31    this.intValue = intValue;
    32}

    33public List getListValue() {
    34    return listValue;
    35}

    36public void setListValue(List listValue) {
    37    this.listValue = listValue;
    38}

    39public Set getSetValue() {
    40    return setValue;
    41}

    42public void setSetValue(Set setValue) {
    43    this.setValue = setValue;
    44}

    45public String[] getArrayValue() {
    46    return arrayValue;
    47}

    48public void setArrayValue(String[] arrayValue) {
    49    this.arrayValue = arrayValue;
    50}

    51public Map getMapValue() {
    52    return mapValue;
    53}

    54public void setMapValue(Map mapValue) {
    55    this.mapValue = mapValue;
    56}

    57   
    58}

    59

    我们可以看到Bean1类中有很多各种各样的属性.相应的创建setter/getter
    2.然后相应配置xml:

     1<?xml version="1.0" encoding="UTF-8"?>
     2<beans
     3    xmlns="http://www.springframework.org/schema/beans"
     4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     6<!-- id名称是随便取的 -->
     7<bean id="bean11" class="com.zyl.spring.Bean1">
     8<!-- name的值是类中定义的变量名 -->
     9 <property name="strValue" value="依赖注入的值"/>
    10 <property name="intValue" value="12311"/>
    11 <property name="listValue">
    12      <list>
    13          <value>list1</value>
    14          <value>list2</value>
    15          <value>list3</value>
    16      </list>
    17 </property>
    18 <property name="setValue">
    19      <set>
    20          <value>set1</value>
    21          <value>set2</value>
    22      </set>
    23 </property>
    24 <property name="arrayValue">
    25      <list>
    26           <value>array1</value>
    27           <value>array2</value>
    28      </list>
    29 </property>
    30 <property name="mapValue">
    31      <map>
    32           <entry key="key1" value="value1"/>
    33           <entry key="key2" value="value2"/>
    34      </map>
    35 </property>
    36 <property name="dateValue">
    37      <value>2008/03/06</value>
    38 </property>
    39</bean>

    注意,属性名(property name)是Bean1类中的变量名哦.
    通过value属性给属性赋值,也可以用ref来指定引用.
    这样就可以了~
    3.我们写一个junit来测试

    package test;

    import junit.framework.TestCase;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.zyl.spring.Bean1;
    import com.zyl.spring.Bean2;


    public class test extends TestCase {
        
    //
        public void testInjection1(){
            BeanFactory factory 
    =new ClassPathXmlApplicationContext("gogogo-*.xml");//加上配置文件xml的名字
           
            Bean1 bean1
    =(Bean1)factory.getBean("bean11");//bean11为xml中取的id名字
            
            System.out.println(
    "strValue"+ bean1.getStrValue());
            System.out.println(
    "intValue"+ bean1.getIntValue());
            System.out.println(
    "listValue"+ bean1.getListValue());
            System.out.println(
    "setValue"+ bean1.getSetValue());
            System.out.println(
    "strArray"+ bean1.getArrayValue());
            System.out.println(
    "mapValue"+ bean1.getMapValue());
            System.out.println(
    "dateValue"+ bean1.getDateValue());
            System.out.println(
    "华丽的分割线-=-=-=-=-=-=-=-=-=-");
            
        }


    注意:BeanFactory factory =new ClassPathXmlApplicationContext("gogogo-*.xml") 这样的写法,可以把xml分开至多个xml中.spring会找到前缀为gogogo-的xml.(bean id不可以重复哦!)
    通过这样的方式就可以打印出效果了.
    可是时间处理却会报错!
    4.我们需要个时间编辑器:UtilDateEdit.java

     1package com.zyl.spring;
     2
     3import java.beans.PropertyEditorSupport;
     4import java.text.SimpleDateFormat;
     5import java.util.Date;
     6
     7public class UtilDateEdit extends PropertyEditorSupport {
     8    //转换时间的功能
     9    private String format;
    10    public String getFormat() {
    11        return format;
    12    }

    13    public void setFormat(String format) {
    14        this.format = format;
    15    }

    16    //private String format="yyyy-MM-dd" ;
    17    public void setAsText(String text) throws IllegalArgumentException {
    18        //将传入的string 转为java.util.date
    19        System.out.println("sdfs"+text);
    20        SimpleDateFormat sdf= new SimpleDateFormat(format);
    21        
    22        try {
    23            Date date =sdf.parse(text);
    24            this.setValue(date);
    25        }
     catch (Exception e) {
    26            // TODO: handle exception
    27        }

    28        
    29        
    30    }

    31 
    32    
    33}

    34


    format的格式可以在xml中配置,让spring来注入吧.
    5.下面看xml中时间编辑器的配置:

     1<bean id="UtilDateEdit666666666" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 这个class是api中 -->
     2<!-- 将属性编辑器注入"<property name="customEditors">"这里面的customEditors是固定的 -->
     3    <property name="customEditors"> <!-- 这个name是固定哦 -->
     4         <map>
     5             <entry key="java.util.Date"> 
     6               <!-- 内部使用的类 -->
     7               <bean class="com.zyl.spring.UtilDateEdit">
     8                  <property name="format" value="yyyy/MM/dd"/>
     9               </bean>
    10             </entry>
    11         </map>
    12    </property>
    13</bean>

    这样就大功告成了.
    打印效果:.(传说中见证奇迹的时刻?)
    sdfs2008/03/06
    strValue依赖注入的值
    intValue12311
    listValue[list1, list2, list3]
    setValue[set1, set2]
    strArray[Ljava.lang.String;@4e280c
    mapValue{key1=value1, key2=value2}
    dateValueThu Mar 06 00:00:00 CST 2008
    华丽的分割线-=-=-=-=-=-=-=-=-=-
    sdfs2008/03/06
    当有时间的处理发生时,spring会在配置文件中找到并调用时间编辑器.至于为什么.....恩..参考下spring的代码吧.
    这次就到这里吧  --end--;

  • 相关阅读:
    并发编程之多线程(理论部分)
    基于TCP和UDP的socket
    ajax笔记 显示出所城市名称 ShowCity.aspx Html代码
    蒸饭的纱布
    ajax 笔记--不刷新实现简单的留言版 guestBook
    从表的第几条取到第几条记录
    在asp.net添加数据到XML里去
    ajax 笔记-- 写了一个不用刷新就能实现--用户名验证的例子
    今天生日
    ajax 笔记不用刷新实现数据的分页显示
  • 原文地址:https://www.cnblogs.com/redcoatjk/p/3562435.html
Copyright © 2011-2022 走看看