bean标签使用

 

1
2
3
4
5
6
7
8
9
10
11
12
/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*vx:it_daimeng
*/

  

 

1. id和name属性

1
2
3
<!--同时添加name和id -->
 
<bean id="drink_01" name="drink_02" class="com.test.pojo.Drink" />

  

Java代码

1
2
3
4
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
/** * 参数1: name/id * 参数2(可选): 可以指定生成对象类型,如果不填此参数,需进行强转 * 两种方式都可以获取! */
Drink drink_01 = applicationContext.getBean("drink_01", Drink.class);
Drink drink_02 = applicationContext.getBean("drink_02", Drink.class);

  

2 scope属性

​ bean标签中添加scope属性,设置bean对应对象生成规则.

2.1 scope = "singleton"

​ 单例,默认值,适用于实际开发中的绝大部分情况.

配置:

1
<bean id="drink_01" name="drink_02" scope="singleton" class="com.test.pojo.Drink" />

  

测试:

1
2
3
4
5
6
7
@Test public void test2(){
//TODO 测试bean标签中 scope = singleton
 
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
Drink drink_01 = applicationContext.getBean("drink_01", Drink.class);
Drink drink_02 = applicationContext.getBean("drink_02", Drink.class); System.out.println(drink_01 == drink_02); //打印 true
}

  

2.2 scope="prototype"

多例,适用于struts2中的action配置

配置:

1
<bean id="drink_01" name="drink_02" scope="prototype" class="com.test.pojo.Drink" />

  

测试 

1
2
3
4
5
6
@Test public void test2(){ //TODO 测试bean标签中 scope = prototype
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
Drink drink_01 = applicationContext.getBean("drink_01", Drink.class);
Drink drink_02 = applicationContext.getBean("drink_02", Drink.class); System.out.println(drink_01 == drink_02); //打印 false
}
点击并拖拽以移动

  

 

3 lazy-init属性

注意: 只对单例有效,设置scope="singleton"时测试

延时创建属性.

lazy-init="false" 默认值,不延迟创建,即在启动时候就创建对象.

lazy-init="true" 延迟初始化,在用到对象的时候才会创建对象.

配置:


<bean id="drink_01" name="drink_02" scope="singleton" lazy-init="false" class="com.test.pojo.Drink" />

测试1: lazy-init="false"

1
2
3
4
5
@Test public void test2(){
//TODO 测试bean标签中的 lazy-init="false" 默认值 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
System.out.println("获取对象之前!");
Drink drink_01 = applicationContext.getBean("drink_01", Drink.class);
Drink drink_02 = applicationContext.getBean("drink_02", Drink.class); System.out.println("获取对象之后!"); //测试结果: 先输出 实体类的构造方法 --> 获取数据之前 --> 获取数据之后 //证明: false 不延迟创建,在创建ApplicationContext的时候就创建了对象! }

  

测试2:lazy-init="true"

1
2
3
4
5
@Test public void test2(){ //TODO 测试bean标签中的 lazy-init="true" 默认值
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
System.out.println("获取对象之前!");
Drink drink_01 = applicationContext.getBean("drink_01", Drink.class);
Drink drink_02 = applicationContext.getBean("drink_02", Drink.class); System.out.println("获取对象之后!"); //测试结果: 先输出 获取数据之前 ---> 实体类的构造方法 --> 获取数据之后 //证明: true 延迟创建, 只有在获取的时候创建. }

  

4 初始化/销毁

在Drink类中添加初始化方法和销毁方法(名称自定义):

1
2
public void init() { System.out.println("Drink的初始化方法"); }
public void destroy() { System.out.println("Drink的销毁方法"); }

  

在配置文件中添加:

1
<bean id="drink_01" name="drink_02" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy" class="com.test.pojo.Drink" />

  

 

posted @   呆萌老师  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示