玩转Spring系列教程06之Bean标签配置属性以及Bean作用域

Spring Bean的bean标签属性配置

<bean>配置属性文件:

id属性和name属性的区别

      id:确定该Bean的唯一标识符,一个Bean代表一个应用组件,Bean的id属性值在整个容器中必须保证唯一。

     name: 为Bean定义一个名字,没有id的情况下, name可以当做id使用

class属性 

Bean的类路径 完整包名+类名。容器会对类进行实例化,所以不能使用接口,应该提供具体的类Scope 属性

Scope 属性

   Bean的作用域配置

lazy-init属性

默认值是false,可以设置为true表示开启懒加载,容器初始化时不会初始化bean,获取对象时再初始化。

factory-bean属性

可以用于指定一个bean的id值,把userId这个bean赋值给userId2这个bean,但是必须加上一个factory-method属性,指定调用一个非静态方法。

factory-bean属性:

<beanid="userId" class="com.itcodeschool.domain.User"factory-method="init" ></bean>

调用User类的inti方法,但是init方法必须是静态的。

init-method属性:

init-mehtod=”方法名”,bean对象初始化时会调用此属性指定的方法;

destroy-method属性:

destroy-method=”方法名”

bean对象销毁时会调用此属性指定的方法,想要看到效果必须ClassPathXmlApplicationContext对象调用close方法;

Spring Bean的bean作用域

Bean的五种作用域

singleton、prototype、request、session、global session

singleton:

单例模式,Spring IOC容器中只会存在一个Bean实例,在容器启动的时候就会初始化所有的单例对象,后期无论多少次获取都是同一个对象。Bean标签的scope模式就是singleton,无需显示指定。

prototype:

原型模式,每次获取Bean实例都会创建一个新的Bean返回,<bean class=”……” scope=”prototype”>。

request:

Bean的作用域在一次Http请求范围内有效, 每一次Http请求都会创建一个新的Bean,<bean class=”……” scope=”request”>。

session:

Bean的作用域在当前一次会话Http Session内有效,同一个Http Session会话多次获取Bean不会重新创建,<bean class=”……” scope=”session”>。

global session

Bean的作用域在全局Session会话内有效,应用于分布式系统Session范围内多次获取获取Bean不会重新创建,仅在使用portlet context时有效。<bean class=”……” scope=” global Session”>。

代码测试如下:

singleton演示:

ApplicationContext.xml

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

 <!--无参构造函数方式 -->

 <bean id="userId"class="com.itcodeschool.domain.User" scope="singleton"></bean>

</beans>

测试代码:

从容器获取3次User对象实例,输出对象的hashCode码,输出hashCode码相同则表示是同一个对象。

    publicvoid test01() {

       //初始化容器

       ApplicationContextcontext = new ClassPathXmlApplicationContext("ApplicationContext.xml");

       //从容器中获取Bean

       Useruser01 = (User) context.getBean("userId");

       Useruser02 = (User) context.getBean("userId");

       Useruser03 = (User) context.getBean("userId");

       //输出对象信息

        System.out.println(user01.hashCode());

        System.out.println(user02.hashCode());

        System.out.println(user03.hashCode());

    }

运行结果如下:

三次获取实例的hashCode码相同,证明三个对象是同一个。

prototype演示:

<bean id="userId"class="com.itcodeschool.domain.User" scope="prototype"></bean>

测试代码:

@Test

    publicvoid test01() {

       //初始化容器

       ApplicationContextcontext = new ClassPathXmlApplicationContext("ApplicationContext.xml");

       //从容器中获取Bean

       Useruser01 = (User) context.getBean("userId");

       Useruser02 = (User) context.getBean("userId");

       Useruser03 = (User) context.getBean("userId");

       //输出对象信息

        System.out.println(user01.hashCode());

        System.out.println(user02.hashCode());

        System.out.println(user03.hashCode());

    }

测试结果:

三次获取对象实例hashCode码不同,说明不是同一个对象。

request、session、global Session需要基于web容器环境,这里就不演示了,自己可以在搭建web环境测试。


猜你喜欢

转载自blog.csdn.net/itcodeschool/article/details/80518098