Spring2.5教程:4、注入依赖

来源:互联网 发布:apache openssl 插件 编辑:程序博客网 时间:2024/05/14 11:02

注入依赖对象

基本类型对象注入(比如属性):

<bean id="orderService" class="cn.itcast.service.OrderServiceBean">

  <constructor-arg index=“0” type=“java.lang.String” value=“xxx”/>//构造器注入

  <property name=“name” value=“zhao/>//属性setter方法注入

</bean>

注入其他bean

方式一

<bean id="orderDao" class="cn.itcast.service.OrderDaoBean"/>

<bean id="orderService" class="cn.itcast.service.OrderServiceBean">

  <property name="orderDao" ref="orderDao"/>

</bean>

方式二(使用内部bean,但该bean不能被其他bean使用)

<bean id="orderService" class="cn.itcast.service.OrderServiceBean">

  <property name="orderDao">

  <bean class="cn.itcast.service.OrderDaoBean"/>

  </property>

</bean>

集合类型的装配

public class OrderServiceBean {

  private Set<String> sets = new HashSet<String>();

  private List<String> lists = new ArrayList<String>();

  private Properties properties = new Properties();

  private Map<String, String> maps = new HashMap<String, String>();

        ....//这里省略属性的gettersetter方法

}

<bean id="order" class="cn.itcast.service.OrderServiceBean">

    <property name="lists">

          <list>

  <value>lihuoming</value>

         </list>

      </property> 

      <property name="sets">

         <set>

            <value>set</value>

        </set>

      </property> 

     <property name="maps">

        <map>

            <entry key="lihuoming" value="28"/>

       </map>

     </property> 

     <property name="properties">

        <props>

  <prop key="12">sss</prop>

       </props>

      </property>

</bean>

 

依赖注入

1使用构造器注入
2使用属性setter方法注入
3使用Field注入(用于注解方式)(字段注入)

注入依赖对象可以采用手工装配自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果。

1.手工装配依赖对象

2.自动装配依赖对象

 

依赖注入--手工装配

手工装配依赖对象,在这种方式中又有两种编程方式

1. xml配置文件中,通过在bean节点下配置,如

<bean id="orderService" class="cn.itcast.service.OrderServiceBean">

  <constructor-arg index=“0” type=“java.lang.String” value=“xxx”/>//构造器注入 index表示是第一个参数,index=“1”是函数的第二个参数

  <property name=“name” value=“zhao/>//属性setter方法注入

</bean>

2. java代码中使用@Autowired@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:(可以使bean.xml配置文件不用那么臃肿)

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

          <context:annotation-config/>

</beans>

这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor(@Autowired)

CommonAnnotationBeanPostProcessor(@Resource)PersistenceAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcessor

      注:@Resource注解在spring安装目录的lib\j2ee\common-annotations.jar

java代码中使用@Autowired@Resource(推荐)注解方式进行装配,这两个注解的区别是:@Autowired默认按类型装配@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。使用annotation不用再写Setter()

    @Autowired

    private PersonDao  personDao;//用于字段上

    @Autowired

    public void setOrderDao(OrderDaoorderDao) {//用于属性的setter方法上

        this.orderDao = orderDao;

    }

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

    @Autowired  @Qualifier("personDaoBean")

    private PersonDao  personDao;

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resourcename属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。

    @Resource(name=“personDaoBean”)

    private PersonDao  personDao;//用于字段上

注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了

推荐使用@Resource,这个不是spring框架提供的,是J2EE提供的。这样的话就不会跟spring框架耦合的太紧密。

 

依赖注入--自动装配依赖对象

对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:

<bean id="..." class="..." autowire="byType"/>

autowire属性取值如下:

lbyType按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null
lbyName按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null
lconstructorbyType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
lautodetect:通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式
原创粉丝点击