spring 攻略 - 笔记- 第三章 Spring中Bean的配置(1)

1.Spring提供2种Ioc容器的实现,基础的实现是BeanFactory,高级的实现是Application Context,它是对BeanFactory的扩展,提供了许多高级的特性,推荐使用Application Context。

BeanFactory的使用

Resource resource = new ClassPathResource("bean.xml") ; //classpath下的bean.xml

//FileSystemResource InputStreamResource UrlResource ClassPathResource....是对Resource的实现

BeanFactory factory = new XmlBeanFactory(resource) ;

Application Context的使用

ApplicationContext appContext = new ClassPathXmlApplication("bean.xml") ;

//ClassPathXmlApplicationContext FileSystemXmlApplicationContext XmlWebApplicationContext

//XmlPortleApplicationContext是对ApplicationContext接口的实现

完整的例子:

ApplicaitonContext constext = new ClassPathXmlApplicationContext("bean.xml");
SequenceGenerator generator =    (SequenceGenerator)context.getBean("sequenceGennerator");
Sytem.out.println(generator.getSequence);

3.3 解决构造器歧义

问题:当配置文件的参数适合多个构造器时,那么匹配构造器就会发生歧义

解决方法:<constructor-arg>元素指定的type 和 index 属性

实现方法:

<bean id="xx" class="xx">

  <constructor-arg type="java.lang.String" value="30" />   #Spring默认将参数是解释为String类型的

  <constructor-arg type="int" value="10" />

  <property name="suffix" value="A" />

</bean>

上面例子的问题:Spring中,参数在XML中出现的顺序是不被考虑的,所以上面的配置适合2个构造函数

解决方法:index属性

<bean id="xx" class="xx">

  <constructor-arg type="java.lang.String" value="30" index="0"/>   #Spring默认将参数是解释为String类型的

  <constructor-arg type="int" value="10" index="1" />

  <property name="suffix" value="A" />

</bean>

 3.4 指定Bean引用

解决方法:<ref>元素或在属性or构造器参数中指定Bean的声明

实现方法:

<bean id="dataPrefixGenerator" class="....">

  <property name="pattern" value="yyyyyMMdd" />

</bean>

 <bean id="sequenceGenerator" class="...">

  <property name=""...

  <property name="prefixGenerator">  # the simple way:<property name="xx' ref="xx" />

     <ref bean="dataPrefixGenerator" />

  </property>

</bean>

 // ref元素可以引用任何的Bean,包括在其它配置文件中的Bean

local属性

 <property name="xx'> <ref local="dataPrefixGenerator" /> </property>

local属性是对XML ID的引用,此时XML 编辑器可以帮助验证拥有该ID的Bean是否存在于同一个XML文件中

3.4.2为构造器指定Bean引用

 <bean id="xx' class="...">

  <constructor-arg>

    <ref local="dataPrefixGenerator" /> # <constructor-arg ref="dataPrefixGenerator" />

  </constructor-arg>

  <property name="initial' value="100" />

</bean>

3.4.3 声明内部的Bean 在<property> or <constructor-arg>  不需要设置任何id或name  # Bean只在声明处有效

<bean id =........

  <property name="prefixGenerator">

    <bean class="com..."> .....</bean>

  </property>

</bean>

 3.5 通过依赖检查来检查属性

3.5.1 问题:IOC中有大量的Bean,此时setter注入的缺点就暴露了,不能保证属性一定会被注入

3.5.2 解决方法:Spring的依赖检查特性可以检查Bean上某些类型的所有属性是否被设置,只需在<Bean>的dependency-check属性指定依赖检查模式。PS:依赖检查属性只能检查属性是否被设置,这与属性值是null的情况,无能为力

dependency-check 属性取值:

none*    默认值,表示所有属性值都不检查

simple   表示如果简单类型(基本类型和集合类型)的属性未被设置,将抛出UnstatisfiedDependencyException

objects  表示对属性是对象类型,但未被设置,将抛出UnstatisfiedDependencyException

 all          表示检查所有属性的值,如果未设置,将抛出UnstatisfiedDependencyException

3.5.3 实现方法:

<bean id="xx" class="..." dependency-check="simple"> <property name=".." ..</bean>

3.6通过@Required注解检查属性

3.6.1 问题:Spring的依赖检查特性dependency-check只能检查某些类型的所有属性。它不能针对单个属性进行检查,大多数情况下,只需检查单个属性

3.6.2 解决方法:RequiredAnnotatinBeanPostProcessor是Spring的Bean后置处理器,它会检查所有具有@Required注解的属性是否已被设置。RequiredAnnotatinBeanPostProcessor是一种特殊的Bean,它能够在每个Bean实例化后执行一些额外的工作。要使用RequiredAnnotatinBeanPostProcessor,必须在Ioc容器中注册它。该处理器同样对属性值是null无能为力。

3.6.3 实现方法:

1.在每个setter方法上加上@Required注解

2.在Ioc容器中注册RequiredAnnotatinBeanPostProcessor实例,如果是BeanFactory,则必须使用API注册,如果是ApplicationContext,那么如下: <bean class="org.springframework.beans.fanctoy.annotation.RequiredAnnotatinBeanPostProcessor />

ps:如果是Spring2.5,那么只需在Bean配置文件声明如下:<context:annotation-config />

如果任何具有@Required注解的属性未被设置,该Bean后置处理器将抛出BeanInitializationExceptin

。。。。。。

除了@Required注解,RequiredAnnotatinBeanPostProcessor也可以对 具有用来定制注解的类的属性进行检查。步骤如下:

1.创建注解类型

2.在类的setter方法上应用此注解

3.在RequiredAnnotatinBeanPostProcessor的required-AnnotationType属性里指定该注解类型

<bean class="org.springframework.beans.factory.annotaion.RequiredAnnotatinBeanPostProcessor">

   <property name="requiredAnnotationType">

      <value>com.xxxx</value>

   </property>

</bean>

猜你喜欢

转载自it-like.iteye.com/blog/1774111
今日推荐