当前位置: 移动技术网 > IT编程>开发语言>Java > Spring中属性注入详解

Spring中属性注入详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

godisagril,袁佳怡献b门全套图,乱世金瓶梅

本文演示了int、string、数组、list、set、map、date等属性的注入。
其中date类型的注入则是借助了spring提供的属性编辑器来实现的,首先是用到的五个实体类

package com.jadyer.model; 
import java.util.date; 
import java.util.list; 
import java.util.map; 
import java.util.set; 
/** 
 * 常见属性的注入 
 * @see 包括int,string,array,list,set,map,date的注入 
 */ 
public class bean11 { 
  private integer intvalue; 
  private string strvalue; 
  private string[] arrayvalue; 
  private list listvalue; 
  private set setvalue; 
  private map mapvalue; 
  private date datevalue; 
  /* 七个属性的setter和getter略 */ 
} 
 
 
package com.jadyer.model; 
public class bean22 { 
  private bean33 bean33; 
  private bean44 bean4422; //注入:与属性名无关,与setbean44()有关 
  private bean55 bean55; 
  /* 三个属性的setter和getter略 */ 
} 
 
 
package com.jadyer.model; 
public class bean33 { 
  private integer id; 
  private string name; 
  private string sex; 
  /* 三个属性的setter和getter略 */ 
} 
 
 
package com.jadyer.model; 
public class bean44 { 
  private integer id; 
  private string name; 
  private string sex; 
  private integer age; 
  /* 四个属性的setter和getter略 */ 
} 
 
 
package com.jadyer.model; 
public class bean55 { 
  private string password; 
  /* 关于password的setter和getter略 */ 
} 

然后是我们自定义的java.util.date类型转换器

package com.jadyer.util; 
 
import java.beans.propertyeditorsupport; 
import java.text.parseexception; 
import java.text.simpledateformat; 
import java.util.date; 
 
/** 
 * java.util.date属性编辑器。相当于类型转换器。这里是将string转为date型 
 * @see ---------------------------------------------------------------------------------------- 
 * @see 该示例主要让大家知道spring也有这种机制,不是让大家以后写属性编辑器 
 * @see 需要写属性编辑器的几率太少了,只要知道spring也有类似的机制就可以了 
 * @see ---------------------------------------------------------------------------------------- 
 * @see 所谓的属性编辑器,就是将spring配置文件中的字符串转换成相应的java对象 
 * @see spring内置了一些属性编辑器,也可以自定义属性编辑器 
 * @see 自定义属性编辑器事,须继承propertyeditorsupport类并覆写setastext()方法 
 * @see 最后再将自定义的属性编辑器注入到spring中,即可 
 * @see ---------------------------------------------------------------------------------------- 
 */ 
public class utildatepropertyeditor extends propertyeditorsupport { 
  private string pattern; //将转换的格式放到配置文件中,让spring注入进来 
  public void setpattern(string pattern) { 
    this.pattern = pattern; 
  } 
   
  @override 
  public void setastext(string text) throws illegalargumentexception { 
    system.out.println("======utildatepropertyeditor.setastext()======" + text); 
    try { 
      date date = new simpledateformat(pattern).parse(text); 
      this.setvalue(date); //注意:这里放进去的是一个java.util.date对象,故输出的时间是默认的格式 
    } catch (parseexception e) { 
      e.printstacktrace(); 
      throw new illegalargumentexception(text); //继续向上抛参数非法的异常 
    } 
  } 
} 

用到的针对所有实体类的applicationcontext-beans.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.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" 
      default-lazy-init="true"> 
  <!-- default-lazy-init="true"属性的说明,详见injectiontest.java类的第49行 --> 
  <!-- default-autowire="byname或bytype",这是spri0ng提供的自动装配bean的两种方式byname和bytype,详解略 --> 
   
  <!-- ***********************【lazy====延迟初始化】*********************************************************************** --> 
  <!-- 执行testinjection22()时默认的会输出======utildatepropertyeditor.setastext()======2010年06月04日 --> 
  <!-- 即此时并未设置default-lazy-init="true",这说明bean11中的datevalue属性的值被注入了 --> 
  <!-- 事实上,默认的spring在创建applicationcontext时,会将配置文件中所有的对象实例化并进行注入 --> 
  <!-- 这样做的好处是如果spring配置文件中的某些配置写错了,它立刻就能检测出来 --> 
  <!-- 而struts1.x的配置文件,如果某个类写错了,是不会出问题的,只有在真正执行的时候,才会出问题 --> 
  <!-- 对于spring而言,也可以采用相关的属性延迟配置文件的初始化,即default-lazy-init="true" --> 
  <!-- 即只有真正使用的时候,再去new这个对象,再为属性注入。这时就涉及到lazy,即延迟初始化 --> 
  <!-- 只需修改spring配置文件即可,如<beans xsi:schemalocation="http://www...." default-lazy-init="true"> --> 
  <!-- 这时的作用范围即整个配置文件,同理也可对各个<bean>标签的lazy-init属性进行单独配置 --> 
  <!-- 但一般都不会这么设置,而是在beanfactory创建的时候,即完成注入,这样也便于检查出错误 --> 
  <!-- ***************************************************************************************************************** --> 
   
  <bean id="bean11" class="com.jadyer.model.bean11"> 
    <property name="intvalue" value="123"/><!-- 注入时,字符串123会自动转换为int型 --> 
    <property name="strvalue" value="hello_spring"/> 
    <property name="arrayvalue"> 
      <list> 
        <value>array11</value> 
        <value>array22</value> 
      </list> 
    </property> 
    <property name="listvalue"> 
      <list> 
        <value>list11</value> 
        <value>list22</value> 
      </list> 
    </property> 
    <property name="setvalue"> 
      <set> 
        <value>set11</value> 
        <value>set22</value> 
      </set> 
    </property> 
    <property name="mapvalue"> 
      <map> 
        <entry key="key11" value="value11"/> 
        <entry key="key22" value="value22"/> 
      </map> 
    </property> 
    <property name="datevalue" value="2010年06月04日"/><!-- 这里date格式应与applicationcontext-editor.xml配置的相同 --> 
  </bean> 
   
  <bean id="bean22" class="com.jadyer.model.bean22"> 
    <property name="bean33" ref="bean33"/> 
    <property name="bean44" ref="bean44"/> 
    <property name="bean55" ref="bean55"/> 
  </bean> 
   
  <bean id="bean55" class="com.jadyer.model.bean55"> 
    <property name="password" value="123"/> 
  </bean> 
</beans> 

用到的针对公共实体类的applicationcontext-common.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.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
       
  <!-- 利用抽象bean提取出公共配置 --> 
  <!-- 首先指定<bean>标签的abstract属性为true,然后在其它<bean>中指定其parent即可 --> 
  <bean id="abstractbean" abstract="true"> 
    <property name="id" value="2"/> 
    <property name="name" value="张起灵"/> 
    <property name="sex" value="男"/> 
  </bean> 
  <bean id="bean33" class="com.jadyer.model.bean33" parent="abstractbean"/> 
  <bean id="bean44" class="com.jadyer.model.bean44" parent="abstractbean"> 
    <property name="age" value="26"/> 
  </bean> 
</beans> 
 
<!-- 使用abstractbean之前的bean33和bean44的原形如下 --> 
<!--  
<bean id="bean33" class="com.jadyer.model.bean33"> 
  <property name="id" value="100"/> 
  <property name="name" value="张三"/> 
  <property name="sex" value="男"/> 
</bean> 
<bean id="bean44" class="com.jadyer.model.bean44"> 
  <property name="id" value="100"/> 
  <property name="name" value="张三"/> 
  <property name="sex" value="男"/> 
  <property name="age" value="90"/> 
</bean> 
 --> 

用到的针对java.util.date属性编辑器的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.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
       
  <bean id="utildatepropertyeditor" class="com.jadyer.util.utildatepropertyeditor"> 
    <property name="pattern" value="yyyy年mm月dd日"/> 
  </bean> 
   
  <!-- 查看源码得知,在customeditorconfigurer类的131行提供了一个setcustomeditors方法,所以就能够注入了 --> 
  <bean id="customeditors" class="org.springframework.beans.factory.config.customeditorconfigurer"> 
    <property name="customeditors"> 
      <map> 
        <entry key="java.util.date" value-ref="utildatepropertyeditor"/> 
      </map> 
    </property> 
  </bean> 
</beans> 
 
<!-- 也可以使用内部<bean>把utildatepropertyeditor写在内部 --> 
<!-- 这样就只有它自己有权使用了,外部就无法使用了 --> 
<!-- 由于不提供外界访问,所以内部<bean>没有id属性 --> 
<!-- 示例如下 --> 
<!--  
<bean id="customeditors" class="org.springframework.beans.factory.config.customeditorconfigurer"> 
  <property name="customeditors"> 
    <map> 
      <entry key="java.util.date"> 
        <bean class="com.jadyer.util.utildatepropertyeditor"> 
          <property name="pattern" value="yyyy年mm月dd日"/> 
        </bean> 
      </entry> 
    </map> 
  </property> 
</bean> 
 -->

 

最后是使用junit3.8写的单元测试类

package com.jadyer.junit; 
 
import junit.framework.testcase; 
 
import org.springframework.context.applicationcontext; 
import org.springframework.context.support.classpathxmlapplicationcontext; 
 
import com.jadyer.model.bean11; 
import com.jadyer.model.bean22; 
 
public class propertyinjectiontest extends testcase { 
  private applicationcontext factory; 
 
  @override 
  protected void setup() throws exception { 
    /****====读取单一的配置文件====****/ 
    //factory = new classpathxmlapplicationcontext("applicationcontext.xml"); 
     
    /****====利用数组读取多个配置文件====****/ 
    //这样就会把两个配置文件作为一个来使用,表面上看是作为两个使用的 
    //其实内部是作为一个使用的,所以在多个配置文件中,里面的id不能重复 
    //但是name属性可以重复,就好像人的身份证编号和名字的区别是一样的 
    //string[] configlocations = new string[]{"applicationcontext.xml", "applicationcontext-editor.xml"}; 
    //factory = new classpathxmlapplicationcontext(configlocations); 
     
    /****=====利用 * 匹配模式读取多个配置文件====****/ 
    //业界流行的一句话:约定优于配置 
    //所以说当有了一个统一的比较好的约定之后,就可以利用框架提供的功能,减少配置量 
    //另外:如果没有读取到applicationcontext-*.xml,此时即便存在applicationcontext.xml,它也不会读的 
    factory = new classpathxmlapplicationcontext("applicationcontext-*.xml"); 
  } 
 
  /** 
   * 该方法演示的是常见属性的注入,包括int,string,array,list,set,map,date的注入 
   * @see 其中date类型的注入则是借助了spring属性编辑器来实现的 
   */ 
  public void testinjection11() { 
    //bean11 bean11 = new bean11(); //若简单的new,那么它的属性是不会被注入的。注入的前提必须是从ioc容器中拿出来的,才会注入 
    bean11 bean11 = (bean11)factory.getbean("bean11"); //此时bean11就是从ioc容器中获取到的,所以它的依赖就会被全部注入 
    system.out.println("bean11.intvalue=" + bean11.getintvalue()); 
    system.out.println("bean11.strvalue=" + bean11.getstrvalue()); 
    system.out.println("bean11.arrayvalue=" + bean11.getarrayvalue()); 
    system.out.println("bean11.listvalue=" + bean11.getlistvalue()); 
    system.out.println("bean11.setvalue=" + bean11.getsetvalue()); 
    system.out.println("bean11.mapvalue=" + bean11.getmapvalue()); 
    system.out.println("bean11.datevalue=" + bean11.getdatevalue()); 
  } 
   
  /** 
   * 该方法主要演示的是将公共的配置进行抽象,以减少配置量 
   */ 
  public void testinjection22() { 
    bean22 bean22 = (bean22)factory.getbean("bean22"); 
    system.out.println("bean22.bean33.id=" + bean22.getbean33().getid()); 
    system.out.println("bean22.bean33.name=" + bean22.getbean33().getname()); 
    system.out.println("bean22.bean33.sex=" + bean22.getbean33().getsex()); 
    system.out.println("bean22.bean44.id=" + bean22.getbean44().getid()); 
    system.out.println("bean22.bean44.name=" + bean22.getbean44().getname()); 
    system.out.println("bean22.bean44.sex=" + bean22.getbean44().getsex()); 
    system.out.println("bean22.bean44.age=" + bean22.getbean44().getage()); 
    system.out.println("bean22.bean55.password=" + bean22.getbean55().getpassword()); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网