一、首先先创建Maven项目,然后在pom.xml文件里添加相应的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.hy.spring</groupId>
<artifactId>SpringDemo2021</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- spring.version -->
<spring.version>5.3.4</spring.version>
</properties>
<dependencies>
<!--Spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring Bean-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
二、创建杀龙任务类
在main/java里创建net.hy.spring.lesson01包,然后在里面创建SlayDragonQuest类、BraveKnight类
SlayDragonQuest类
package net.hy.spring.lesson01;
/*
* 功能:杀龙任务类
* 作者:黄运
* 日期:2021年03月24日
*/
public class SlayDragonQuest {
public void embark() {
System.out.println("执行杀龙任务。");
}
}
BraveKnight类
package net.hy.spring.lesson01;
/**
* 功能:勇敢骑士类
* 作者:黄运
* 日期:2021年03月24日
*/
public class BraveKnight {
private SlayDragonQuest slayDragonQuest;
public void setSlayDragonQuest(SlayDragonQuest slayDragonQuest) {
this.slayDragonQuest = slayDragonQuest;
}
public void embarkOnQuest() {
slayDragonQuest.embark();
}
}
三、使用传统方法完成杀龙任务
在test/java里创建net.hy.spring.lesson01包,然后在里面创建TestBraveKnightOld类,然后运行测试方法,查看相应的结果。
TestBraveKnightOld类代码块
package net.hy.spring.lesson01;
import org.junit.Test;
/**
* 功能:传统方式测试勇敢骑士类
* 作者:黄运
* 日期:2021年03月24日
*/
public class TestBraveKnightOld {
@Test
public void testBraveKnight() {
//创建杀龙任务对象
SlayDragonQuest slayDragonQuest = new SlayDragonQuest();
//创建勇敢骑士对象
BraveKnight braveKnight = new BraveKnight();
//设置勇敢骑士杀龙任务属性
braveKnight.setSlayDragonQuest(slayDragonQuest);
//勇敢骑士执行任务
braveKnight.embarkOnQuest();
}
}
测试运行结果
四、采用Spring框架完成杀龙任务
首先在resources目录里创建log4j.properties日志文件以及创建Spring配置文件spring-config.xml(我这里的配置文件是拆分了的)
spring-config01.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="slayDragonQuest" class="net.hy.spring.lesson01.SlayDragonQuest"/>
<bean id="Mike" class="net.hy.spring.lesson01.BraveKnight">
<property name="slayDragonQuest" ref="slayDragonQuest"/>
</bean>
</beans>
spring-config02.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="rescueDamselQuest" class="net.hy.spring.lesson01.RescueDamselQuest"/>
<bean id="damselRescuingKnight" class="net.hy.spring.lesson01.DamselRescuingKnight">
<constructor-arg ref="rescueDamselQuest"/>
</bean>
</beans>
然后创建TestKnight类编写测试方法
package net.hy.spring.lesson01;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestKnight {
private ClassPathXmlApplicationContext context;//基于类路径XML配置文件的应用容器
@Before
public void init(){
//基于Spring配置文件创建应用容器
context = new ClassPathXmlApplicationContext( "xmlconfig/spring-config*.xml" );
}
@Test
public void testKnight() {
//根据名称从应用容器获取骑士对象
BraveKnight mike = (BraveKnight) context.getBean( "Mike" );
DamselRescuingKnight damselRescuingKnight = (DamselRescuingKnight) context.getBean( "damselRescuingKnight" );
//让骑士们执行任务
mike.embarkOnQuest();
damselRescuingKnight.embarkOnQuest();
}
}
最后测试运行结果