Spring Bean的装配方式 第3关:自动装配 Bean

28 篇文章 9 订阅

目录

任务描述

相关知识

什么是自动装配

Bean 元素的 autowire 属性

自动装配实例

编程要求

测试说明

参考答案


任务描述

本关任务:使用自动装配 Bean 的方式使给定程序正确运行。

相关知识

为了完成本关任务,你需要掌握: 1.什么是自动装配; 2.Bean 元素的 autowire 属性; 3.自动装配的使用。

什么是自动装配

除了使用 XML 和注解的方式装配 Bean 以外,还有一种自动装配的方式。自动装配就是指 Spring 容器可以自动装配相互协作的 Bean 之间的关联关系,将一个 Bean 注入其他 Bean 的 Property 中。一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些。

Bean 元素的 autowire 属性

如果要使用自动装配,需要配置 Bean 元素的 autowire 属性。该属性有五个值,如下所示:

名称说明
byName根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。当一个 Bean 节点带有 autowire 的属性时,将查找其类中所有的 set 方法名,获得将 set 去掉并且首字母小写的字符串,然后去 spring 容器中寻找是否有此字符串名称 id 的对象。如果有,就取出注入;如果没有,就报空指针异常。
byType根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。
constructor根据构造方法的参数的数据类型,进行 byType 模式的自动装配。
autodetect如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。
no默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。

自动装配实例

下面通过修改上一关卡(基于注解装配 Bean)中的案例,演示如何实现自动装配。首先将所有文件中的注解去掉或者注释,然后将 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="studentDao" class="com.educoder.springtest.StudentDaoImpl" />
    <bean id="studentService" class="com.educoder.springtest.StudentServiceImpl" autowire="byName" />
    <bean id="studentAction" class="com.educoder.springtest.StudentAction" autowire="byName" />
</beans>

在上述配置文件中,用于配置 studentService 和 studentAction 的 bean 元素中除了 id 和 class 属性以外,还增加了 autowire 属性,并将其属性值设置为 byName(按属性名称自动装配)。

默认情况下,配置文件中需要通过 ref 装配 Bean,但设置了 autowire="byName",Spring 会在配置文件中自动寻找与属性名字 studentDao 相同的 bean,找到后,通过调用 setStudentDao(StudentDao studentDao)方法将 id 为 studentDao 的 Bean 注入 id 为 studentService 的 Bean 中,这时就不需要通过 ref 装配了。

执行结果:

  1. 执行Dao层的show()方法
  2. 执行Service层的show()方法
  3. 执行Action层的show()方法

从结果中可以看出,使用自动装配的方式同样完成了依赖注入。

编程要求

仔细阅读右侧编辑区内给出的代码框架及注释,在 Begin-End 间编写代码,使用自动装配 Bean 的方式使给定程序正确运行,部分说明及要求如下:

  • 程序会在后台获得 studentAction 实例,并调用 show() 方法,在该方法中会调用 studentServiceImpl 实例的 toTime()方法,该方法又会调用 studentDaoImpl 实例的 showTables()方法;

  • 在指定文件(StudentDaoImpl.java、StudentServiceImpl.java、StudentAction.java、applicationContext.xml)的指定位置修改或补充代码使程序运行,所有代码文件可在右侧代码编辑区的“代码文件”处可见。

测试说明

平台将使用测试集运行你编写的程序代码,若全部的运行结果正确,则通关。

可在右侧 “测试结果”区查看具体的测试集详情。

测试输入:

预期输出:

  1. 执行Dao层的add()方法
  2. 执行Service层的add()方法
  3. 执行Action层的add()方法

开始你的任务吧,祝你成功!

参考答案

StudentAction.java

package com.educoder.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
// 请在此处修改代码
/********* Begin *********/
@Controller("studentAction")
/********* End *********/

public class StudentAction {
    // 请在此处修改代码
    /********* Begin *********/
    @Resource(name = "studentService")   
    /********* End *********/

    private StudentService studentService;
    public StudentService getStudentService() {
        return studentService;
    }
    public void setStudentService(StudentService studentService ){
        this.studentService=studentService;
    }


    public void show() {
        // 调用 personService 中的 show() 方法
        studentService.toTime();
        System.out.println("执行Action层的show()方法");
    }
}

StudentServiceImpl.java

package com.educoder.springtest;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
// 请在此处修改代码
/********* Begin *********/
@Service("studentService")
/********* End *********/

public class StudentServiceImpl implements StudentService {
    // 请在此处修改代码
    /********* Begin *********/
    @Resource(name = "studentDao")    
    /********* End *********/

    private StudentDao studentDao;
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public void setStudentDao(StudentDao studentDao){
        this.studentDao=studentDao;
    }
    // 重写 toTime 方法
    @Override
    public void toTime() {
        // 调用 studentDao 中的 showTables()方法
        studentDao.showTables();
        System.out.println("执行Service层的toTime()方法");
    }
}

StudentDaoImpl.java

package com.educoder.springtest;

import org.springframework.stereotype.Repository;
// 请在此处修改代码
/********* Begin *********/
@Repository("studentDao")
/********* End *********/

public class StudentDaoImpl implements StudentDao{
    // 重写 showTables 方法
    @Override
    public void showTables() {
        System.out.println("执行Dao层的showTables()方法");
    }
}

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">
    <!-- 请在此处编写代码 -->
    <!--********* Begin *********-->
    <bean id="studentDao" class="com.educoder.springtest.StudentDaoImpl" />
    <bean id="studentService" class="com.educoder.springtest.StudentServiceImpl" autowire="byName" />
    <bean id="studentAction" class="com.educoder.springtest.StudentAction" autowire="byName" />
    <!--********* End *********-->

</beans>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目录 第一部分spring的核心 第1章开始spring之旅 1.1spring是什么 1.2开始spring之旅 1.3理解依赖注入 1.3.1依赖注入 1.3.2di应用 1.3.3企业级应用中的依赖注入 1.4应用aop 1.4.1aop介绍 1.4.2aop使用 1.5小结 第2章基本bean装配 2.1容纳你的bean 2.1.1beanfactory介绍 2.1.2使用应用上下文 2.1.3bean的生命 2.2创建bean 2.2.1声明一个简单的bean 2.2.2通过构造函数注入 2.3注入bean属性 2.3.1注入简单的数值 2.3.2使用其他的bean 2.3.3装配集合 2.3.4装配空值 2.4自动装配 2.4.1四种自动装配类型 2.4.2混合使用自动和手动装配 2.4.3何时采用自动装配 2.5控制bean创建 2.5.1bean范围化 2.5.2利用工厂方法来创建bean 2.5.3初始化和销毁bean 2.6小结 第3章高级bean装配 3.1声明父bean和子bean 3.1.1抽象基bean类型 3.1.2抽象共同属性 3.2方法注入 3.2.1基本的方法替换 3.2.2获取器注入 3.3注入非springbean 3.4注册自定义属性编辑器 3.5使用spring的特殊bean 3.5.1后处理bean 3.5.2bean工厂的后处理 3.5.3配置属性的外在化 3.5.4提取文本消息 3.5.5程序事件的解耦 3.5.6让bean了解容器 3.6脚本化的bean 3.6.1给椰子上lime 3.6.2脚本化bean 3.6.3注入脚本化bean的属性 3.6.4刷新脚本化bean 3.6.5编写内嵌的脚本化bean 3.7小结 第4章通知bean 4.1aop简介 4.1.1定义aop术语 4.1.2spring对aop的支持 4.2创建典型的spring切面 4.2.1创建通知 4.2.2定义切点和通知者 4.2.3使用proxyfactorybean 4.3自动代理 4.3.1为spring切面创建自动代理 4.3.2自动代理@aspectj切面 4.4定义纯粹的pojo切面 4.5注入aspectj切面 4.6小结 第二部分企业spring 第5章使用数据库 5.1spring的数据访问哲学 5.1.1了解spring数据访问的异常体系 5.1.2数据访问的模板化 5.1.3使用dao支持类 5.2配置数据源 5.2.1使用jndi数据源 5.2.2使用数据源连接池 5.2.3基于jdbc驱动的数据源 5.3在spring里使用jdbc 5.3.1处理失控的jdbc代码 5.3.2使用jdbc模板 5.3.3使用spring对jdbc的dao支持类 5.4在spring里集成hibernate 5.4.1选择hibernate的版本 5.4.2使用hibernate模板 5.4.3建立基于hibernate的dao 5.4.4使用hibernate3上下文会话 5.5springjava持久api 5.5.1使用jpa模板 5.5.2创建一个实体管理器工厂 5.5.3建立使用jpa的dao 5.6spring和ibatis 5.6.1配置ibatis客户模板 5.6.2建立基于ibatis的dao 5.7缓存 5.7.1配置缓存方案 5.7.2缓存的代理bean 5.7.3注解驱动的缓存 5.8小结 第6章事务管理 6.1理解事务 6.1.1仅用4个词解释事务 6.1.2理解spring对事务管理的支持 6.2选择事务管理器 6.2.1jdbc事务 6.2.2hibernate事务 6.2.3jpa事务 6.2.4jdo事务 6.2.5jta事务 6.3在spring中编写事务 6.4声明式事务 6.4.1定义事务参数 6.4.2代理事务 6.4.3在spring2.0里声明事务 6.4.4定义注释驱动事务 6.5小结 第7章保护spring 7.1springsecurity介绍 7.2验证用户身份 7.2.1配置providermanager 7.2.2根据数据库验证身份 7.2.3根据ldap仓库进行身份验证 7.3控制访问 7.3.1访问决策投票 7.3.2决定如何投票 7.3.3处理投票弃权 7.4保护web应用程序 7.4.1代理springsecurity的过滤器 7.4.2处理安全上下文 7.4.3

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于建章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值