目录
任务描述
本关任务:使用自动装配 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 装配了。
执行结果:
执行Dao层的show()方法
执行Service层的show()方法
执行Action层的show()方法
从结果中可以看出,使用自动装配的方式同样完成了依赖注入。
编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,在 Begin-End 间编写代码,使用自动装配 Bean 的方式使给定程序正确运行,部分说明及要求如下:
-
程序会在后台获得 studentAction 实例,并调用 show() 方法,在该方法中会调用 studentServiceImpl 实例的 toTime()方法,该方法又会调用 studentDaoImpl 实例的 showTables()方法;
-
在指定文件(StudentDaoImpl.java、StudentServiceImpl.java、StudentAction.java、applicationContext.xml)的指定位置修改或补充代码使程序运行,所有代码文件可在右侧代码编辑区的“代码文件”处可见。
测试说明
平台将使用测试集运行你编写的程序代码,若全部的运行结果正确,则通关。
可在右侧 “测试结果”区查看具体的测试集详情。
测试输入:
无
预期输出:
执行Dao层的add()方法
执行Service层的add()方法
执行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>