一、创建新包
- 在
net.xxr.spring
包创建day03子包

二、拷贝类与接口
- 将
day02
子包的类与接口拷贝到day03
子包

三、创建注解配置类
- 在
day03子
包里创建SpringConfig
类,取代Spring配置文件

package net.xxr.spring.day03;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("net.xxr.spring.day03")
public class SpringConfig {
}
四、创建测试类
- 在
test/java
里创建net.xxr.spring.day03
包,在包里创建TestKnight
类

package net.xxr.spring.day03;
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() {
Knight knight1 = (Knight) context.getBean("RobinHood");
knight1.embarkOnQuest();
Knight knight2 = (Knight) context.getBean("rescueDamselKnight");
knight2.embarkOnQuest();
}
@After
public void destroy() {
context.close();
}
}
五、运行测试类
