Spring设置注入和构造注入详解

来源:互联网 发布:js获取文本框的值 编辑:程序博客网 时间:2024/05/16 10:41

在Spring中注入方式有设置注入和构造注入。设置注入就是指要被注入的类中定义有一个setter()方法,并在参数中定义需要注入的对象。简单的看个例子。

定义有User类,并覆写了toString()方法。

[java] view plaincopy
  1. package com.zcl.spring.setterinjection;  
  2.   
  3. public class User {  
  4.     private String name ;  
  5.     private int age ;  
  6.     private String country ;  
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public void setAge(int age) {  
  17.         this.age = age;  
  18.     }  
  19.     public String getCountry() {  
  20.         return country;  
  21.     }  
  22.     public void setCountry(String country) {  
  23.         this.country = country;  
  24.     }  
  25.     public String toString(){  
  26.         return name + " is " + age + " years old,living in " + country ;   
  27.     }  
  28. }  
配置beans.xml文件,通过设置注入为类中属性注入值

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xsi:schemaLocation="http://www.springframework.org/schema/beans   
  3.     http://www.springframework.org/schema/beans/spring-beans.xsd"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xmlns="http://www.springframework.org/schema/beans">   
  6.     <bean id="user" class="com.zcl.spring.setterinjection.User">  
  7.         <property name="name" value="Zhao" />  
  8.         <property name="age" value="22" />  
  9.         <property name="country" value="China" />  
  10.     </bean>  
  11. </beans>  

测试一下:

[java] view plaincopy
  1. package com.zcl.spring.setterinjection;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Main {  
  7.     public static void main(String args[]){  
  8.         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;  
  9.         User user = (User)context.getBean("user") ;  
  10.         System.out.println(user) ;  
  11.     }  
  12. }  

输入结果:

[java] view plaincopy
  1. Zhao is 22 years old,living in China  

现在我们来看写构造注入,所谓构造注入就是指要被注入的类中声明一个构造方法,并在此方法的参数中定义要注入的对象。修改下刚刚的User类。

[java] view plaincopy
  1. package com.zcl.spring.setterinjection;  
  2.   
  3. public class User {  
  4.     private String name ;  
  5.     private int age ;  
  6.     private String country ;  
  7.       
  8.     public User(String name, int age, String country) {  
  9.         this.name = name;  
  10.         this.age = age;  
  11.         this.country = country;  
  12.     }  
  13.   
  14.     public String toString(){  
  15.         return name + " is " + age + " years old,living in " + country ;   
  16.     }  
  17. }  

配置beans.xml文件:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xsi:schemaLocation="http://www.springframework.org/schema/beans   
  3.     http://www.springframework.org/schema/beans/spring-beans.xsd"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xmlns="http://www.springframework.org/schema/beans">   
  6.     <bean id="user" class="com.zcl.spring.setterinjection.User">  
  7.         <constructor-arg value="Zhao" />  
  8.         <constructor-arg value="22" />  
  9.         <constructor-arg value="China" />  
  10.     </bean>  
  11. </beans>  

测试函数一样,打印结果也一样。


ref元素是用在property中,来设置需要引用的容器管理的其它Bean。
 
   它的用法:<ref  bean|local|parent="someBean">,这里主要分析一下这三个参数的作用。
 
   这次先看实例,再进行讲解。
 
·  先建立一个包:javamxj.spring.basic.ref  ,然后把以下5个文件放在这个包下。
HelloBean.java  

Java代码  收藏代码
  1. package javamxj.spring.basic.ref;  
  2.   
  3. public class HelloBean {  
  4.     private String hello;  
  5.   
  6.     public String getHello() {  
  7.         return hello;  
  8.     }  
  9.   
  10.     public void setHello(String hello) {  
  11.         this.hello = hello;  
  12.     }  
  13. }  

 

HelloDate.java  

Java代码  收藏代码
  1. package javamxj.spring.basic.ref;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class HelloDate {  
  6.     private Date date;  
  7.   
  8.     private HelloBean hb;  
  9.   
  10.     public void setDate(Date date) {  
  11.         this.date = date;  
  12.     }  
  13.   
  14.     public void setHb(HelloBean hb) {  
  15.         this.hb = hb;  
  16.     }  
  17.   
  18.     public void sayHello() {  
  19.         System.out.println(hb.getHello() + "  " + date.toLocaleString());  
  20.     }  
  21.   
  22. }   

 

beans.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="GBK"?>   
  2. <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.       
  5.     <bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">  
  6.         <property name="hello" value="Hello! Child Bean." />  
  7.     </bean>  
  8.       
  9.     <bean id="dateBean" name="#date" class="java.util.Date"/>  
  10.       
  11.     <bean id="hd1" class="javamxj.spring.basic.ref.HelloDate">  
  12.         <property name="hb">  
  13.             <ref bean="helloBeanParent"/>  
  14.         </property>  
  15.         <property name="date">  
  16.             <ref bean="#date"/>  
  17.             <!--<ref bean="dateBean"/>-->  
  18.         </property>  
  19.     </bean>  
  20.       
  21.     <bean id="hd2" class="javamxj.spring.basic.ref.HelloDate">  
  22.         <property name="hb">  
  23.             <ref local="helloBean"/>  
  24.         </property>  
  25.         <property name="date">  
  26.             <ref local="dateBean"/>  
  27.         </property>  
  28.     </bean>  
  29.       
  30.     <bean id="hd3" class="javamxj.spring.basic.ref.HelloDate">  
  31.         <property name="hb">  
  32.             <ref parent="helloBean"/>  
  33.         </property>  
  34.         <property name="date">  
  35.             <bean class="java.util.Date"/>  
  36.         </property>  
  37.     </bean>  
  38.       
  39. </beans>  

 

parent.xml  

Java代码  收藏代码
  1. <?xml version="1.0" encoding="GBK"?>   
  2. <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.       
  5.     <bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">  
  6.         <property name="hello" value="Hello! Javamxj." />  
  7.     </bean>  
  8.       
  9.     <bean id="helloBeanParent" class="javamxj.spring.basic.ref.HelloBean">  
  10.         <property name="hello" value="Hello! Parent Bean." />  
  11.     </bean>  
  12.       
  13. </beans>   

 

Main.java  

Java代码  收藏代码
  1. package javamxj.spring.basic.ref;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.ClassPathResource;  
  6.   
  7. public class Main {  
  8.   
  9.     public static void main(String[] args) {  
  10.         BeanFactory parent = new XmlBeanFactory(new ClassPathResource(  
  11.                 "javamxj/spring/basic/ref/parent.xml"));  
  12.         BeanFactory child = new XmlBeanFactory(new ClassPathResource(  
  13.                 "javamxj/spring/basic/ref/beans.xml"), parent);  
  14.   
  15.         HelloDate hd1 = (HelloDate) child.getBean("hd1");  
  16.         HelloDate hd2 = (HelloDate) child.getBean("hd2");  
  17.         HelloDate hd3 = (HelloDate) child.getBean("hd3");  
  18.   
  19.         hd1.sayHello();  
  20.         hd2.sayHello();  
  21.         hd3.sayHello();  
  22.     }  
  23. }   

 

·运行Main.java,输出结果如下:
 
Hello!  Parent  Bean.    2005-8-10  22:25:56
Hello!  Child  Bean.    2005-8-10  22:25:56
Hello!  Javamxj.    2005-8-10  22:25:56
 
 
     OK!这里主要分析beans.xml、Main.java这两个文件。对于Main.java要注意的是如何加载“parent”的,重点看看beans.xml中ref元素的用法。
 
   首先定义两个bean:helloBean、dateBean,分别指向HelloBean类和Date类。然后定义了hd1、hd2、hd3等三个bean,都指向HelloDate类。
 
·hd1:
   property标签中的“helloBeanParent”并不存在于beans.xml中,而是在parent.xml中,使用<ref  bean="someBean">可以从其它bean配置文件中寻找需要加载的目标bean。bean属性的值可以同目标bean的id属性相同,也可以同它的name属性中任意的一个值相同。
 
·hd2:
     property标签中的“helloBean”如果不存在于beans.xml中,则XML解析器会提示错误。<ref  local="someBean">只能这个bean配置文件中寻找需要加载的目标bean,而且local属性值必须同目标bean的id属性相同。
 
·hd3:
     property标签中的“helloBean”同时存在于beans.xml和parent.xml中,使用<ref  bean="someBean">则会优先从beans.xml中寻找需要加载的目标bean,如果需要从parent.xml中加载目标bean,则需使用<ref  parent="someBean">。在设置date时,使用的是内联bean,这时可以不要任何id或name定义。


 

spring idref和ref的区别

分类: 编程技术 1454人阅读 评论(0) 收藏 举报
springbeanclassxml

idref元素用来将容器内其它bean的id传给<constructor-arg/> 或 <property/>元素,同时提供错误验证功能。

<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
    <property name="targetName">
        <idref bean="theTargetBean" />
    </property>
</bean>

上述bean定义片段完全地等同于(在运行时)以下的片段

<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
    <property name="targetName" value="theTargetBean" />
</bean> 

 也是就是说idref我可以获取spring容器中的bean的name的值(一个字符串),而不是bean的实例。

idref元素的功能与<value>类似,就是idref多了验证的功能,减少配置的书写错误机率。除了<idref bean=""/>,如果被引用的bean在同一个xml文件中,且bean的名字就是bean的id,除了可以使用<idfef local=""/>,此属性允许xml解析器在解析XML的时候对引用的bean进行验证。

value方式,传给client bean的targetName属性值并没有被验证。任何的输入错误仅在client bean实际实例化时才会被发现(可能伴随着致命的错误)。


而ref是获取这个bean的实例。用来实现注入功能。

假如只是想获取bean的名称 采用idref

使用idref标记允许容器在部署时 验证所被引用的bean是否存在。



ref有三个属性:local、parent、bean,具体区别如下:

  • local只能指定与当前配置的对象在同一个配置文件的对象定义的名称;
  • parent则只能指定位于当前容器的父容器中定义的对象引用;
  • bean则基本上通吃,所以,通吃情况下,直接使用bean来指定对象引用就可以了。

0 0
原创粉丝点击