美文网首页
Spring Bean配置项(二)

Spring Bean配置项(二)

作者: lqsss | 来源:发表于2018-03-21 20:48 被阅读0次

Id:在整个ioc容器中这个bean的唯一标示
Class:这个bean的实现类
Scope:bean的范围,作用域
Constructor arguments:构造器的参数 构造注入
Properties:属性 设值注入
Autowiring mode:自动装载
lazy-initialization mode:懒加载模式
initialization/destruction method...:初始化和销毁方法

Bean的作用域

Bean的作用域(每个作用域都是在同一个Bean容器中)

1.singleton:单例,指一个Bean容器中只存在一份(默认)

2.prototype:每次请求(每次使用)创建新的实例,destory方式不生效

3.request:每次http请求创建一个实例且仅在当前request内生效(只能在web中使用)

4.session:同上,每次http请求创建一个实例,当前session内有效(只能在web中使用)

5.global session:基于portlet的web中有效(portlet定义了global session),如果是在单个web中,同session。(只能在web中使用)

singleton

<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean>
    @Test
    public void testSay() {
        BeanScope beanScope = super.getBean("beanScope");
        beanScope.say();
        
        BeanScope beanScope2 = super.getBean("beanScope");
        beanScope2.say();
    }

BeanScope say : 727001376
BeanScope say : 727001376

prototype

<bean id="beanScope" class="com.imooc.bean.BeanScope" scope="prototype"></bean>

结果不同

相关文章

网友评论

      本文标题:Spring Bean配置项(二)

      本文链接:https://www.haomeiwen.com/subject/ugenqftx.html