一、利用组件注解符精简Spring配置文件
(一)创建新包
创建net.huawei.spring.day02包
(二)复制四个类
(三)修改杀龙任务类
package net. huawei. spring. day02 ;
import org. springframework. stereotype. Component ;
@Component
public class SlayDragonQuest {
public void embark ( ) {
System . out. println ( "执行杀龙任务……" ) ;
}
}
(四)修改救美任务类
package net. huawei. spring. day02 ;
import org. springframework. stereotype. Component ;
@Component
public class RescueDamselQuest {
public void embark ( ) {
System . out. println ( "执行救美任务……" ) ;
}
}
(五)修改勇敢骑士类
package net. huawei. spring. day02 ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. stereotype. Component ;
@Component ( "RobinHood" )
public class BraveKnight {
@Value ( "罗宾汉" )
private String name;
@Autowired
private SlayDragonQuest slayDragonQuest;
public void embarkOnQuest ( ) {
System . out. print ( "勇敢骑士[" + name + "]" ) ;
slayDragonQuest. embark ( ) ;
}
}
添加组件注解符,设置参数“RobinHood”,对应的就是Spring配置文件里装配Bean元素的id属性值。 注意:删除setSlayDragonQuest()方法,因为已经通过注解符设置了勇敢骑士的姓名和杀龙任务属性。 查看@Component注解源码
(六)修改救美骑士类
package net. huawei. spring. day02 ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. stereotype. Component ;
@Component
public class RescueDamselKnight {
@Value ( "格拉海德" )
private String name;
@Autowired
private RescueDamselQuest rescueDamselQuest;
public void embarkOnQuest ( ) {
System . out. print ( "救美骑士[" + name + "]" ) ;
rescueDamselQuest. embark ( ) ;
}
}
(七)创建Spring配置文件
< ? 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 = "net.huawei.spring.day02" / >
< / beans>
(八)创建测试类
package net. huawei. spring. day02 ;
import org. junit. After ;
import org. junit. Before ;
import org. junit. Test ;
import org. springframework. context. support. ClassPathXmlApplicationContext ;
public class TestKnight {
private ClassPathXmlApplicationContext context;
@Before
public void init ( ) {
context = new ClassPathXmlApplicationContext ( "xml_annotation/spring-config.xml" ) ;
}
@Test
public void testKnight ( ) {
BraveKnight knight1 = ( BraveKnight ) context. getBean ( "RobinHood" ) ;
knight1. embarkOnQuest ( ) ;
RescueDamselKnight knight2 = ( RescueDamselKnight ) context. getBean ( "rescueDamselKnight" ) ;
knight2. embarkOnQuest ( ) ;
}
@After
public void destroy ( ) {
context. close ( ) ;
}
}
二、程序优化 - 面向接口
(一)创建任务接口
package net. huawei. spring. day02 ;
public interface Quest {
void embark ( ) ;
}
(二)创建骑士接口
package net. huawei. spring. day02 ;
public interface Knight {
void embarkOnQuest ( ) ;
}
(三)修改杀龙任务类
(四)修改救美任务类
(五)修改勇敢骑士类
注意:SlayDragonQuest类改成了Quest接口,这样就可以给该骑士自动装配任何任务(实现了Quest接口的Bean),增加了程序的灵活性。
(六)修改救美骑士类
(七)修改测试类
(八)运行测试类