zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean

    除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配。自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间的关联关系,将一个 Bean 注入其他 Bean 的 Property 中。
    要使用自动装配,就需要配置 <bean> 元素的 autowire 属性。autowire 属性有五个值,具体如下:
    byName    根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。
    byType    根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。
    constructor    根据构造方法的参数的数据类型,进行 byType 模式的自动装配。
    autodetect    如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。
    no    默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。
    将 applicationContext.xml 配置文件修改成自动装配形式,如下所示。
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        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/aop 
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
        <bean id="personDao" class="com.mengma.annotation.PersonDaoImpl" />
        <bean id="personService" class="com.mengma.annotation.PersonServiceImpl"
            autowire="byName" />
        <bean id="personAction" class="com.mengma.annotation.PersonAction"
            autowire="byName" />
    </beans>
    在上述配置文件中,用于配置 personService 和 personAction 的 <bean> 元素中除了 id 和 class 属性以外,还增加了 autowire 属性,并将其属性值设置为 byName(按属性名称自动装配)。
    默认情况下,配置文件中需要通过 ref 装配 Bean,但设置了 autowire="byName",Spring 会在配置文件中自动寻找与属性名字 personDao 相同的 <bean>,找到后,通过调用 setPersonDao(PersonDao personDao)方法将 id 为 personDao 的 Bean 注入 id 为 personService 的 Bean 中,这时就不需要通过 ref 装配了。
    使用 JUnit 再次运行测试类中的 test() 方法,控制台的显示结果如图

  • 相关阅读:
    最大并发连接数和最大会话数的区别
    Redis Sentinel 情况下bind地址设置
    ZooKeeper 授权验证
    推荐一个zookeeper信息查看工具
    WebForm-博客园-6.0-空间(Space)-短信息(Msg)
    ylbtech-cnblogs(博客园)-数据库设计-6.0-Msg(短消息)
    WebForm-博客园-1.0-账户模块(Passport)-登录与注册
    WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
    ylbtech-cnblogs(博客园)-数据库设计-1,Passport(账户)
    IIS 配置
  • 原文地址:https://www.cnblogs.com/tszr/p/12129720.html
Copyright © 2011-2022 走看看