Spring 不曾知道的事儿

来源:互联网 发布:土地整理预算软件 编辑:程序博客网 时间:2024/04/25 08:33

1、Spring 通过工厂方法创建Bean


如果要装配的对象需要通过静态方法来创建(如单例模式 没有公开的构造方法,取而代之的是,静态方法getInstance),可以通过<bean>元素的factory-method属性来装配工厂创建的Bean,如:

<bean id="testObj" class="com.test.TestObj"  factory-method="getInstance" />


2、Bean的作用域

    所有的Spring Bean默认都是单例。当容器分配一个Bean时(不论是通过装配还是调用容器的getBean()方法),它总是返回Bean的同一个实例。

Spring的Bean作用域 允许用户配置所创建的Bean属于哪一种作用域,而无需在Bean的实现里硬编码作用域规则,如下:

作用域:singleton  定义:在每一个Spring容器中,一个Bean定义只有一个对象实例(默认)

作用域:prototype 定义:允许Bean的定义可以被实例化任意次(每次调用都创建一个实例)

作用域:request   定义:在一次HTTP请求中,每个Bean定义对应一个实例,该作用域仅在基于web的Spring上下文(例如Spring mvc)中才有效

作用域:session 定义:在一个HTTP Session中,每个Bean定义对应一个实例。该作用域仅在基于Web的Spring上下文(例如Spring mvc)中才有效

作用域:global-session  定义:在一个全局HTTP Session中,每个Bean定义对应一个实例。该作用域仅在Portlet上下文中才有效


3、初始化和销毁Bean

          3.1、 为Bean定义初始化和销毁操作,只需要使用init-method和destory-method参数来配置<bean>元素。init-method属性指定了在初始化Bean时要调用的方法,destory-method属性指定了Bean从容器移除之前要调用的方法。如:

<bean id="testObj" class="com.test.TestObj"  init-method="begin"  destory-method="end"  />

如果在上下文中定义的很多Bean都拥有相同名字的初始化方法和销毁方法,可以使用<beans>元素的default-init-method和default-destory-method属性,如:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" 
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"


     default-init-method="begin"

     default-destory-method="end"

>

</beans>


        3.2、另一种方式

       


4、注入内部Bean

      内部Bean并不仅限于setter注入,还可以装配到构造方法的入参中,事例:

 setter注入:

<bean id="testObj" class="com.test.TestObj"   >

<property name="testPro">

<bean class="com.test.sub.SubTestObj"  />

         </property>

</bean>

装配构造方法:

<bean id="testObj" class="com.test.TestObj"   >

<constructor-arg>

<bean class="com.test.sub.SubTestObj"  />

</constructor-arg>

</bean>

5、使用Spring的命名空间p装配属性



选择<property>还是命名空间p取决于你,它们是等价的。

6、Spring  装配集合



装配List、Set和Array

<bean id="testObj" class="com.test.TestObj"   >

     <property name="obj">

           <list>或<set>

<ref bean="obj1" />

<ref bean="obj2" />

  </list>或</set>

     </property>

</bean>


装配map

<bean id="testObj" class="com.test.TestObj"   >

     <property name="obj">

           <map>

<entry key="key1" value-ref="value1"  />

<entry key="key2" value-ref="value2"  />

  </map>

     </property>

</bean>

PS:<map>中的<entry>元素由一个键和一个值组成,键和值可以是简单类型,也可以是其他Bean的引用。如下表:



装配properties集合

<bean id="testObj" class="com.test.TestObj"   >

     <property name="obj">

           <props>

<prop key="key1">value1</prop>

<prop key="key2">value2</prop>

  </props>

     </property>

</bean>


7、装配空值

      虽然通常情况下Bean属性的最初值都是null,直到你为它赋值,但是有些Bean会为它的属性默认值设置一个非空值。如果因为某些特殊原因,我们需要把属性设置为null值,这种场景下,如果我们不能完全确定属性的值会为null,那么就必须显示地为该属性装配一个null值。

        为属性设置null值,只需使用<null/>元素。例如:

    <property name="testObj"><null/></property>


8、使用表达式装配

Spring3引入了Spring表达式语言(Spring Expression Language),拥有很多特性:

1)使用Bean的ID来引用Bean;2)调用方法和访问对象的属性;3)对值进行算术、关系和逻辑运算;4)正则表达式匹配;5)集合操作。

SpEL的基本原理

SpEL表达式的首要目标是通过计算获得某个值。在计算这个数值的过程中,会使用到其他的值并会对这些值进行操作。最简单的spEL求值或许是对字面值、Bean的属性或者某个类的常量进行求值。

字面值:<property name="testPro" value="#{5}" />    注: #{}标记的内容是SpEL表达式

引用Bean、Properties和方法:

bean:<property name="testPro" value="#{pro1}" /> 等同于<property name="testPro" ref="pro1" />

属性:<property name="testPro" value="#{obj.pro1}" />

方法:<property name="testPro" value="#{obj.getPro1()}" />

         判空操作:<property name="testPro" value="#{obj.getPro1()?.toUpperCase()}" />  注:?.运算符表示如果左边值为null则直接返回,否则继续执行



Spring自动装配



如:<bean id="testObj" class="com.test.TestObj"   autowire="byName|constructor|autodetect"></bean>




0 0
原创粉丝点击