Spring基础(二)之手动装配JavaBean

来源:互联网 发布:在淘宝上怎么兼职手模 编辑:程序博客网 时间:2024/06/13 02:01

有两种方式装配Bean(也算是spring提供的两种容器:beanfactory和applicationcontext):

1)Bean工厂(Beanfactory)

2)应用上下文(ApplicationContext)

第一种方法使用org.springframework.beans.factory.BeanFactory接口来获得Bean对象实例。在Spring中有很多BeanFactory接口的实现,单最常用的是org.springframework.beans.factory.xml.XmlBeanFactory。通过XmlBeanFactory类可以从XML配置文件中读取Bean的装配信息,并在Spring容器中建立相应的JavaBean对象实例,并返回该JavaBean对象实例。

要想建立一个XmlBeanFactory对象实例,需要将配置文件通过FileSystemResource对象传入XmlBeanFactory类的构造方法。并通过BeanFactory类的getBean方法获得Bean的对象实例。eg:

package edu.xaut.jzd.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.FileSystemResource;

public class HelloWorldTest {
public static void main(String[] args){
//装配applicationContext.xml文件
BeanFactory factory=new XmlBeanFactory("src\\applicationContext.xml");
//获得被装配的HelloWorld对象实例
HelloWorld helloWorld = (HelloWorld)factory.getBean("helloworld");
System.out.println(helloWorld.getGreeting());
}
}

第二种方法通过org.springframework.context.ApplicationContext接口来装配Bean。在Spring中有如下两种ApplicationContext的实现经常用到。

FileSystemXmlApplicationContext:通过绝对或相对路径指定XML配置文件,并装载xml文件的配置信息。

ClassPathXmlApplicationContext:从类路径中搜索xml配置文件。可以使用FileSystemXmlApplicationContext类替换ClassPathXmlApplicationContext,但要将构造方法的参数值改为“applicationContext.xml”。

单从装配Bean上看,ApplicationContext和Beanfactory类似,但ApplicationContext比Beanfactory提供了更多的功能,如国际化、装载文件资源、向监听器Bean发送事件等。因此,如果要使用更多的功能,最好使用ApplicationContext来装配Bean。

装配普通属性时使用<property>和<value>标签即可完成装配工作。eg:

<bean id="helloworld" class="edu.xaut.jzd.spring.HelloWorld">
        <property name="greeting" >
        <value>xautjzd</value>
        </property>
</bean>

一般不需要指定属性的类型(Spring会通过java的反射机制确定属性的类型),单如果需要,可以使用<value>标签的type属性来指定属性类型。

如果属性类型是另外一个被装载的类,则有两种方法装配其属性值:

1)使用bean标签:

<bean id="helloworld" class="edu.xaut.jzd.spring.HelloWorldImpl">
        <property name="greeting" >
        <bean>
        <property name="
hello">
        <value>xautjzd</value>
        </property>
        </bean>
        </property>
       </bean>

2)使用<ref>标签:

  1. <bean id="collectionBean" class="com.tyq.collection.CollectionBean"    
  2.             parent="abstractCollectionBean">  
  3.             <property name="list">  
  4.                 <list merge="true" value-type="java.lang.Object">  
  5.                     <value>list1</value>  
  6.                     <ref local="collectionBean" />  
  7.                     <null></null>  
  8.                 </list>  
  9.             </property>  
  10.     </bean>  

<ref>标签容易和<idref>标签混淆。在spring 中idref是得到一个bean的id的string值而ref得到的是一个bean的实例
  1. <bean id="collectionBean" class="com.tyq.collection.CollectionBean"    
  2.             parent="abstractCollectionBean">  
  3.             <property name="list">  
  4.                 <list merge="true" value-type="java.lang.String">  
  5.                     <value>list1</value>  
  6.                     <idref local="collectionBean" />  
  7.                     <null></null>  
  8.                 </list>  
  9.             </property>  
  10.     </bean>  
这时加入list中的只是一个值为collectionBean的String实例而上面的ref标签加入的才是一个CollectionBean实例

在Spring中也可以装配集合类型属性,Spring支持的集合属性有:List、Set、Map和Properties。与这四种集合类型相对应的标签是<list>、<set>、<map>和<pros>。在装配集合类型属性时注意两点:
1)Bean的集合属性不需要使用new来建立对象实例,spring容器根据xml配置文件中的装配信息自动来实例化这些属性。
2)Spring分别为上述四种集合类型指定了如下的特定集合类来实例化。
<list>:java.util.ArrayList;
<set>:java.util.LinkedHashSet;
<map>:java.util.LinkedHashMap;
<props>:java.util.Properties;
Spring容器在自动实例化集合属性时,将使用上面的相应集合类来实例化相应的属性,读者在进行类型转换时应注意这一点。

装配构造方法
并不是每个JavaBean都只有一个无参构造方法,如果一个JavaBean的构造方法的参数有一个或多个,就需要使用<constructor-arg>标签来为这些构造方法设置相应的参数值。Spring搜索Bean的构造方法时,会将参数值都当成String类型数据看,除非明确指定了某一参数的类型。默认情况下,spring会根据<constructor-arg>的type属性来确定参数类型。同时也可以改变传递参数的顺序,用<constructor-arg>标签的index属性来指定,从0开始。eg:
<bean id="helloworld" class="edu.xaut.jzd.spring.HelloWorld">
        <property name="greeting" >
        <value>xautjzd</value>
        </property>
        <constructor-arg index="1">
        <value>hello</value>
        </constructor-arg>
        <constructor-arg index="0">
        <value>xautjzd</value>
        </constructor-arg>
       </bean>