戴着假发的程序员出品
alias标签
spring应用手册(第一部分)
alias从字面意思理解就是别名。而这个标签的实际作用就是给 bean添加别名。
看下面案例:
<!-- 注册accountService -->
<bean id="accountService" name="a_service"
autowire="byType" class="com.dk.demo1.service.AccountService"/>
<!-- 给accountService添加别名。-->
<alias name="accountService" alias="sub1service"/>
<alias name="a_service" alias="sub2service"/>
测试:
@Test
public void testSpringAlias(){
//创建spring容器
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) ac.getBean("sub1service");
accountService.save("戴着假发的程序员");
}
这个标签往往用在一个主系统的某个bean需要多个子系统引用时,我们会为每个子系统设置一个别名。