1.18 alias标签

戴着假的程序员出品

[查看视频教程]

alias从字面意思理解就是别名。而这个标签的实际作用就是给bean添加别名。

看下面案例:

1     <!-- 注册accountService -->
2     <bean id="accountService" name="a_service"
3           autowire="byType"  class="com.dk.demo1.service.AccountService"/>
4     <!-- 给accountService添加别名。-->
5     <alias name="accountService" alias="sub1service"/>
6     <alias name="a_service" alias="sub2service"/>

测试:

1     @Test
2     public void testSpringAlias(){
3         //创建spring容器
4         ClassPathXmlApplicationContext ac =
5                 new ClassPathXmlApplicationContext("applicationContext.xml");
6         AccountService accountService = (AccountService) ac.getBean("sub1service");
7         accountService.save("戴着假发的程序员");
8     }

这个标签往往用在一个主系统的某个bean需要多个子系统引用时,我们会为每个子系统设置一个别名。

posted @ 2020-10-04 10:46  戴着假发的程序员0-1  阅读(221)  评论(0编辑  收藏  举报