该文章主要为完成实训任务,详细实现过程及结果见【http://t.csdn.cn/1fwEO】
文章目录
一、采用Java配置类管理Bean
1.1 创建子包
- 在
cn.kox.spring
包里创建day04
子包
1.2 创建杀龙任务类
- 在day04子包里创建杀龙任务类 -
SlayDragonQuest
package cn.kox.spring.day04;
public class SlayDragonQuest {
public void embark() {
System.out.println("执行🗡🏹💣🐉任务...");
}
}
1.3 创建勇敢骑士类
- 在day04子包里创建勇敢骑士类 -
BraveKnight
package cn.kox.spring.day04;
public class BraveKnight {
private String name; // 骑士姓名
private SlayDragonQuest slayDragonQuest; // 杀龙任务
public void setName(String name) {
this.name = name;
}
// 设置杀龙任务
public void setSlayDragonQuest(SlayDragonQuest slayDragonQuest) {
this.slayDragonQuest = slayDragonQuest;
}
// 勇敢骑士执行任务
public void embarkOnQuest() {
System.out.print("勇敢骑士[" + name + "]"); // 骑士留名
slayDragonQuest.embark(); // 执行杀龙任务
}
}
1.4 创建Spring配置类
- 在day04子包里创建Spring配置类 -
SpringConfig
package cn.kox.spring.day04;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 表明是Spring配置类
public class SpringConfig {
@Bean // 创建名为slayDragonQuest的Bean
public SlayDragonQuest slayDragonQuest() {
return new SlayDragonQuest();
}
@Bean // 创建名为braveKnight的Bean
public BraveKnight braveKnight() {
BraveKnight braveKnight = new BraveKnight();
braveKnight.setName("罗宾汉");
braveKnight.setSlayDragonQuest(slayDragonQuest());
return braveKnight;
}
}
1.5 创建测试类
- 在
/test/java
里创建net.huawei.spring.day04
包,在包里创建TestKnight
类
package cn.kox.spring.day04;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestKnight {
private AnnotationConfigApplicationContext context; // 基于注解配置类的应用容器
@Before
public void init() {
// 基于注解配置类创建应用容器
context = new AnnotationConfigApplicationContext(SpringConfig.class);
}
@Test
public void testKnight() {
// 根据名称从应用容器里获取勇敢骑士对象
BraveKnight knight = (BraveKnight) context.getBean("braveKnight");
// 勇敢骑士执行任务
knight.embarkOnQuest();
}
@After
public void destroy() {
// 关闭应用容器
context.close();
}
}
1.6 运行测试类
二、课堂练习
2.1 创建救美任务类和救美骑士类
- RescueDamselQuest类 - 救美任务类
package cn.kox.spring.day04;
public class RescueDamselQuest {
public void embark() {
System.out.println("执行👮️💕👰任务...");
}
}
- RescueDamselKnight类 - 救美骑士类
package cn.kox.spring.day04;
public class RescueDamselKnight {
private String name;
private RescueDamselQuest rescueDamselQuest;
public void setName(String name) {
this.name = name;
}
public void setRescueDamselQuest(RescueDamselQuest rescueDamselQuest) {
this.rescueDamselQuest = rescueDamselQuest;
}
public void embarkOnQuest() {
System.out.print("勇敢骑士[" + name + "]");
rescueDamselQuest.embark();
}
}
2.2 修改Spring配置类 - 管理救美任务Bean和救美骑士Bean
package cn.kox.spring.day04;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 表明是Spring配置类
public class SpringConfig {
@Bean // 创建名为slayDragonQuest的Bean
public SlayDragonQuest slayDragonQuest() {
return new SlayDragonQuest();
}
@Bean
public RescueDamselQuest rescueDamselQuest() {
return new RescueDamselQuest();
}
@Bean // 创建名为braveKnight的Bean
public BraveKnight braveKnight() {
BraveKnight braveKnight = new BraveKnight();
braveKnight.setName("罗宾汉");
braveKnight.setSlayDragonQuest(slayDragonQuest());
return braveKnight;
}
@Bean
public RescueDamselKnight rescueDamselKnight() {
RescueDamselKnight rescueDamselKnight = new RescueDamselKnight();
rescueDamselKnight.setName("尼古拉四德中间带个K");
rescueDamselKnight.setRescueDamselQuest(rescueDamselQuest());
return rescueDamselKnight;
}
}
2.3 在testKnight()方法里添加救美骑士
package cn.kox.spring.day04;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestKnight {
private AnnotationConfigApplicationContext context; // 基于注解配置类的应用容器
@Before
public void init() {
// 基于注解配置类创建应用容器
context = new AnnotationConfigApplicationContext(SpringConfig.class);
}
@Test
public void testKnight() {
// 根据名称从应用容器里获取勇敢骑士对象
BraveKnight knight = (BraveKnight) context.getBean("braveKnight");
// 勇敢骑士执行任务
knight.embarkOnQuest();
RescueDamselKnight knight1 = (RescueDamselKnight) context.getBean("rescueDamselKnight");
knight1.embarkOnQuest();
}
@After
public void destroy() {
// 关闭应用容器
context.close();
}
}