如何正确运用bean标签进行配置?

2026-05-29 08:143阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计858个文字,预计阅读时间需要4分钟。

如何正确运用bean标签进行配置?

/*作者:老和尚认证讲师:CSDN认证讲师高级讲师:51CTO高级讲师认证讲师:腾讯课堂认证讲师认证讲师:网易云课堂认证讲师认证讲师:华为开发者学院认证讲师认证讲师:爱奇艺千人行名师在这里给家长分享技术、知识”


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

1. id和name属性

<!--同时添加name和id -->

<bean id="drink_01" name="drink_02" class="com.test.pojo.Drink"

Java代码

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);

如何正确运用bean标签进行配置?

2 scope属性

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

2.1 scope = "singleton"

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

配置:

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

测试:

@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配置

配置:

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

测试

@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"

@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"

@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类中添加初始化方法和销毁方法(名称自定义):

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

在配置文件中添加:

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

更多关注

​​edu.51cto.com/topic/3338.html​​

本文共计858个文字,预计阅读时间需要4分钟。

如何正确运用bean标签进行配置?

/*作者:老和尚认证讲师:CSDN认证讲师高级讲师:51CTO高级讲师认证讲师:腾讯课堂认证讲师认证讲师:网易云课堂认证讲师认证讲师:华为开发者学院认证讲师认证讲师:爱奇艺千人行名师在这里给家长分享技术、知识”


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

1. id和name属性

<!--同时添加name和id -->

<bean id="drink_01" name="drink_02" class="com.test.pojo.Drink"

Java代码

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);

如何正确运用bean标签进行配置?

2 scope属性

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

2.1 scope = "singleton"

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

配置:

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

测试:

@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配置

配置:

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

测试

@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"

@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"

@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类中添加初始化方法和销毁方法(名称自定义):

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

在配置文件中添加:

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

更多关注

​​edu.51cto.com/topic/3338.html​​