创建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.lj.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>
使用属性注入方式为Bean注入属性
1.创建杀龙任务类SlayDragonQuest.java
2.创建勇敢骑士类 BraveKnight.java
/**
* 勇敢骑士类
*/
public class BraveKnight {
private SlayDragonQuest slayDragonQuest;
public void setSlayDragonQuest(SlayDragonQuest slayDragonQuest) {
this.slayDragonQuest = slayDragonQuest;
}
public void embarkOnQuest(){
slayDragonQuest.embark();
}
}
3.在resources目录里创建log4j.properties
log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4.创建Spring配置文件spring-config.xml
5.在Spring配置文件里创建Bean
第一个Bean对象是第二个Bean对象的属性值,Bean的id不允许重复
<!-- 杀龙任务Bean-->
<bean id="slayDragonQuest" class="net.lj.spring.lesson01.SlayDragonQuest"/>
<!-- 勇敢骑士Bean-->
<bean id="Mike" class="net.lj.spring.lesson01.BraveKnight">
<property name="slayDragonQuest" ref="slayDragonQuest"/>
</bean>
</bean>
6.创建测试类TestBraveKnightNew .java
import net.lj.spring.lesson01.BraveKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Spring容器测试勇敢骑士类
*/
public class TestBraveKnightNew {
private ClassPathXmlApplicationContext context;//基于类路径xml配置文件的原因容器
@Before
public void init(){
//基于Spring配置文件创建原因容器
context = new ClassPathXmlApplicationContext("xmlconfig/spring-config.xml");
//提示用户
System.out.println("Spring应用容器已启动");
}
@Test
public void testBraveKnight(){
//根据名称从应用容器中获取勇敢骑士对象
BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");
//勇敢骑士执行任务
braveKnight.embarkOnQuest();
}
@After
public void destroy(){
//关闭应用容器
context.close();
//提示用户
System.out.println("Spring应用容器已关闭");
}
}
测试结果
使用构造函数注入方式为Bean注入属性
1.创建拯救少女任务类 RescueDamselQuest.java
创建拯救少女之骑士类 2.DamselRescuingKnight.java
3.在Spring配置文件里创建拯救少女骑士Bean
<!-- 拯救少女任务类Bean-->
<bean id="rescueDamselQuest" class="net.lj.spring.lesson01.RescueDamselQuest"/>
<!-- 拯救少女骑士类Bean-->
<bean id="damselRescuingKnight" class="net.lj.spring.lesson01.DamselRescuingKnight">
<constructor-arg ref="rescueDamselQuest"/>
</bean>
4.创建测试类TestDamselRescuingKnightNew.java
import net.lj.spring.lesson01.DamselRescuingKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 采用Spring容器测试拯救少女骑士类
*/
public class TestDamselRescuingKnightNew {
private ClassPathXmlApplicationContext context;
@Before
public void init() {
// 基于Spring配置创建应用容器
context = new ClassPathXmlApplicationContext("xmlconfig/spring-config.xml");
//提示用户
System.out.println("Spring应用容器已启动");
}
@Test
public void testDamselRescuingKnight() {
// 根据名称从应用容器里获取拯救少女骑士对象
DamselRescuingKnight damselRescuingKnight = (DamselRescuingKnight) context.getBean("damselRescuingKnight");
// 拯救少女骑士执行任务
damselRescuingKnight.embarkOnQuest();
}
@After
public void destroy() {
// 关闭应用容器
context.close();
//提示用户
System.out.println("Spring应用容器已关闭");
}
}