Spring 2.X 深入理解 ->Bean的基本操作

Spring 2.X 深入了解 ------->Bean的基本操作

Spring Framework Module
1.Core Container:the Core,Beans,Context,and Expression Language modules
2.Data Access/Integration:JDBC,ORM,OXM,JMS and Transaction modules
3.Web :Web Web-Servlet,Web-Struts and Web-Porlet modules
4.AOP and Instrumentation
5.Test


Ioc容器:Inversion of control
依赖包: org.springframework,beans和org.springframework.context包

 

下面我们具体的说明怎么实现Spring2.5关于bean的操作

jar:spring-beans.jar,spring-context.jar,spring-core.jar,commons-logging-1.1.1.jar

下面介绍如何配置Spring的XML
有二种方式:
1.通过dtd方式配置XML
2.通过xsd方式配置XML
两种方式分别加上一下语句即可:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
              "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

具体的配置我这里给出例子:
<beans>
    <alias name="fromName" alias="toName" />

    <import resource="services.xml" />
    <import resource="resources/messageSource.xml" />
      <import resource="/resources/themeSource.xml" />

    <bean id="bean1" class="com.chenhailong.bean.People">
    </bean>
   
    <bean id="serviceLocator" class="com.foo.DefaultServiceLocator" />

    <bean id="exampleBean1" factory-bean="serviceLocator"
        factory-method="createInstance" />
</beans>

public class SpringBean {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationMap.xml" });
        BeanFactory factory = context;
        People peo1 = (People) factory.getBean("bean1");
        peo1.setSex(true);
        System.out.println(peo1.isSex());
        Resource res = new FileSystemResource("cfg/applicationMap.xml");
        BeanFactory factory1 = new XmlBeanFactory(res);
        People peo = (People) factory1.getBean("bean1");
        peo.setName("cenhailong");
        System.out.println(peo.getName());
    }

}
 


NOTICE:
比如说,在com.example包下有一个叫 Foo的类,而Foo类有一个静态 的内部类叫Bar,那么在bean定义的时候, class属性必须这样写:
com.example.Foo$Bar
注意这里我们使用了$字符将内部类和外部类进行分隔

当采用构造器来创建bean实例时,Spring对class并没有特殊的要求, 我们通常使用的class都适用。也就是说,被创建的类并不需要实现任何特定的 接口,或以特定的方式编码,只要指定bean的class属性即可。不过根据所采用 的IoC类型,class可能需要一个默认的空构造器。

 

第一种形式也是最常见的形式是通过使用<ref/>标记指定bean属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML 'bean'元素的值既可以是指定bean的id值也可以是其name值。

<ref bean="someBean"/>
第二种形式是使用ref的local属性指定目标bean,它可以利用XML解析器来验证所引用的bean是否存在同一文件中。local属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,XML解析器将抛出一个例外。如果目标bean是在同一文件内,使用local方式就是最好的选择(为了尽早地发现错误)。

<ref local="someBean"/>
第三种方式是通过使用ref的parent属性来引用当前容器的父容器中的bean。parent属性值既可以是目标bean的id值,也可以是name属性值。而且目标bean必须在当前容器的父容器中。使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean(例如,子上下文中的一个bean定义覆盖了他的父bean)。

通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。
<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
    <!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService"  <-- notice that the name of this bean is the same as the name of the 'parent' bean
      class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="target">
          <ref parent="accountService"/>  <-- notice how we refer to the parent bean
      </property>
    <!-- insert other configuration and dependencies as required as here -->
</bean>

<bean id="moreComplexObject" class="example.ComplexObject">
  <!-- results in a setAdminEmails(java.util.Properties) call -->
  <property name="adminEmails">
    <props>
        <prop key="administrator">administrator@example.org</prop>
        <prop key="support">support@example.org</prop>
        <prop key="development">development@example.org</prop>
    </props>
  </property>
  <!-- results in a setSomeList(java.util.List) call -->
  <property name="someList">
    <list>
        <value>a list element followed by a reference</value>
        <ref bean="myDataSource" />
    </list>
  </property>
  <!-- results in a setSomeMap(java.util.Map) call -->
  <property name="someMap">
    <map>
        <entry>
            <key>
                <value>an entry</value>
            </key>
            <value>just some string</value>
    </entry>
        <entry>
            <key>
                <value>a ref</value>
            </key>
            <ref bean="myDataSource" />
        </entry>
    </map>
  </property>
  <!-- results in a setSomeSet(java.util.Set) call -->
  <property name="someSet">
    <set>
        <value>just some string</value>
        <ref bean="myDataSource" />
    </set>
  </property>
</bean>

下面的例子展示了集合合并特性:
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.com</prop>
            <prop key="support">support@example.com</prop>
        </props>
    </property>
</bean>
<bean id="child" parent="parent">
    <property name="adminEmails">
        <!-- the merge is specified on the *child* collection definition -->
        <props merge="true">
            <prop key="sales">sales@example.com</prop>
            <prop key="support">support@example.co.uk</prop>
        </props>
    </property>
</bean>
<beans>
在上面的例子中,childbean的adminEmails属性的<props/>元素上使用了merge=true属性。当child bean被容器实际解析及实例化时,其 adminEmails将与父集合的adminEmails属性进行合并。

<null/>用于处理null值。Spring会把属性的空参数当作空字符串处理。以下的xml片断将email属性设为空字符串。

<bean class="ExampleBean">
  <property name="email"><value/></property>
</bean>
这等同于Java代码: exampleBean.setEmail("")。而null值则可以使用<null>元素可用来表示。例如:

<bean class="ExampleBean">
  <property name="email"><null/></property>
</bean>
上述的配置等同于Java代码:exampleBean.setEmail(null)。