
spring(4)——给beans中的bean对象取别名的两种方式,alias标签和bean的name属性
发布日期:2021-05-07 02:55:46
浏览次数:27
分类:精选文章
本文共 1158 字,大约阅读时间需要 3 分钟。
1.alias标签
package com.lixv.dao;import com.lixv.entity.Hello;import com.lixv.entity.HelloSpring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloSpring hellospring1 = (HelloSpring) context.getBean("helloSpringNew"); System.out.println(hellospring1); }}
- 这样就可以通过helloSpringNew来获取helloSpring这个对象
- 仍然可以通过helloSpring来获取helloSpring这个对象
2.通过bean的name属性来取别名
public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloSpring hellospring1 = (HelloSpring) context.getBean("helloSpringNew1"); System.out.println(hellospring1); }}
- 可以通过helloSpringNew1或者helloSpringNew2来获取helloSpring对象。
- bean的name属性可以一次性设置多个别名,用逗号分号空格分开皆可。
- 仍然可以通过alias设置的别名和bean的id来获取这个对象
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月14日 21时21分12秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
上周热点回顾(4.24-4.30)
2019-03-06
[故障公告]博客站点1台负载均衡遭遇流量攻击,造成联通与移动用户无法正常访问
2019-03-06
上周热点回顾(5.1-5.7)
2019-03-06
上周热点回顾(5.29-6.4)
2019-03-06
上周热点回顾(6.19-6.25)
2019-03-06
云计算之路-阿里云上:docker swarm 集群故障与异常
2019-03-06
上周热点回顾(2.19-2.25)
2019-03-06
云计算之路-阿里云上:博客web服务器轮番CPU 100%
2019-03-06
云计算之路-阿里云上:服务器CPU 100%问题是memcached连接数限制引起的
2019-03-06
上周热点回顾(3.26-4.1)
2019-03-06
故障公告:IIS应用程序池停止工作造成博客站点无法访问
2019-03-06
【故障公告】极验验证码故障造成无法登录与注册
2019-03-06
上周热点回顾(6.25-7.1)
2019-03-06
【故障公告】10:30-10:45 左右 docker swarm 集群节点问题引发故障
2019-03-06
工作半年的思考
2019-03-06
不可思议的纯 CSS 滚动进度条效果
2019-03-06
【CSS进阶】伪元素的妙用--单标签之美
2019-03-06
开始CN的生活
2019-03-06
惊闻NBC在奥运后放弃使用Silverlight
2019-03-06