一、课程目标
- 掌握采用组件注解符精简Spring配置文件
- 理解面向接口优化程序结构的思想
- 掌握采用组件注解符精简Spring配置文件
理解面向接口优化程序结构的思想 - 配置 - configuration
- 注解 - annotation
二、打开项目【SpringDemo】
三、利用组件注解符精简Spring配置文件
1、创建day02包
2、将day01子包的四个类拷贝到day02子包
3、修改杀龙任务类 - SlayDragonQuest
- 业务Bean的配置可用注解符:
@Component
- 组件 (@Service
- 服务、@Repository
- 仓库、@Mapper
- 映射器、@Controller
- 控制器)
4、修改救美任务类 - RescueDamselQuest
5、修改勇敢骑士类 - BraveKnight
- 注意:删除setSlayDragonQuest()方法,因为已经通过自动装配注解设置了勇敢骑士的杀龙任务属性。
6、修改救美骑士类 - DamselRescuingKnight4
- 注意:删除了构造方法,因为已经通过资源注解符将救美任务属性注入了救美骑士组件。
7、创建Spring配置文件
- 在
resources
目录创建xml_annotation
子目录,然后在里面创建Spring配置文件spring-config.xml
- 组件扫描:扫描指定包下添加了注解符的类(
@Component
、@Service
、@Repository
、@Mapper
、@Controller
),将其生成Bean
对象
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--组件扫描,扫描指定包下添加注解符的类和接口,将其生成bean对象-->
<context:component-scan base-package="day02"/>
</beans>
8、创建测试类 - TestKnight
- 在test/java里创建net.hw.spring.lesson2包,在包里创建TestKnight类
package net.ls.spring.day02;
import day02.BraveKnight;
import day02.RescueDamselknight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 功能:测试骑士类
* 作者:ls
* 日期:2023年02月20日16时36分25秒
*/
public class TestKnight {
private ClassPathXmlApplicationContext context; //基于类路径XML配置文件的应用容器
@Before
public void init(){
//基于spring配置文件创建应用容器
context = new ClassPathXmlApplicationContext("xml_annotation/spring-config.xml");
}
@Test
public void testknight(){
//根据名称从应用容器获取勇敢骑士对象
BraveKnight knight = (BraveKnight) context.getBean("RobinHood");
//勇敢骑士执行任务
knight.embarkOnQuest();
//根据名称从应用容器获取救美骑士对象
RescueDamselknight rescueDamselknight = (RescueDamselknight) context.getBean("rescueDamselknight");
//救美骑士执行任务
rescueDamselknight.embarkOnQuest();
}
@After
public void destroy(){
//关闭应用容器
context.close();
}
}
- 运行测试方法testKnight(),查看结果
四、程序优化 - 面向接口
- Spring框架可以方便地管理Bean及其相互依赖。为了模块之间实现松耦合,一般采用面向接口的方式。多种骑士,多种任务,可以任意搭配。为了实现这个效果,我们应该抽象出两个接口:骑士接口(Knight)和任务接口(Quest)。骑士接口有两个实现类:
BraveKnight
和DamselRescuingKnight
;任务接口有两个实现类:SlayDragonQuest
和RescueDamselQuest
。
(一)创建任务接口
- 任务接口 -
Quest
package net.ls.spring.day03;
/**
* 功能:任务接口
* 作者:ls
* 日期:2023年02月20日17时37分29秒
*/
public interface Quest {
void embark();
}
(二)创建骑士接口
- 骑士接口 - Knight
package net.ls.spring.day03;
/**
* 功能:骑士接口
* 作者:ls
* 日期:2023年02月20日17时39分23秒
*/
public interface Knight {
void embarkOnQuest();
}
(三)修改杀龙任务类
- 杀龙任务类 -
SlayDragonQuest
,实现任务接口
(四)修改救美任务类
- 救美任务类 -
RescueDamselQuest
,实现任务接口
(五)修改勇敢骑士类
-
勇敢骑士类 -
BraveKnight
,实现骑士接口
-
注意:SlayDragonQuest类改成了Quest接口,这样就可以给该骑士自动装配任何任务(实现了Quest接口的Bean),增加了程序的灵活性。
(六)修改救美骑士类
- 救美骑士类 -
DamselRescuingKnight
,实现骑士接口
(七)修改测试类
- 测试类 -
TestKnight
(八)运行测试类
- 运行测试方法 - testKnight
五、课堂练习
任务1、两种骑士交换执行任务。
- 将勇敢骑士类和救美骑士类中的执行任务变量交换
- 修改勇敢骑士类
- 修改救美骑士类
- 运行结果