spring中bean的配置项(4)

2016/1/16 10:38:54


  • bean的配置项
  • bean的作用域
  • bean的生命周期
  • bean的自动装配
  • Resources&ResourceLoader

1.bean的配置项(常用)

  • Id:整个IOC容器中bean的唯一标识
  • Class:具体要实例化的类
  • Scope:是单例还是多例
  • Constructor arguments:构造参数
  • Properties:成员变量
  • Autowiring mode:自动装配模式
  • lazy—initialization mode:懒加载模式
  • Initialization/destruction method:初始化和销毁的方法

2.bean的作用域

  • singleton(默认模式):单例,指一个bean容器中只存在一份
  • prototype:每次请求(每次使用)创建新的实例,destroy方式不生效
  • request:每次http请求创建一个实例且仅在当前request内有效
  • session:同上,每次http请求创建,当前session中有效
  • global session:基于portlet的web中有效(portlet定义了global sessio),如果在web中,同session

Scope测试

XML:spring-beanScope.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" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <bean id="beanScope" class="com.zjx.bean.BeanScope"></bean>

</beans>

bean类

package com.zjx.bean;

public class BeanScope {
    public void say(){
        // 哈希码是bean的唯一标识
        System.out.println("BeanScope say:"+this.hashCode());
    }
}

测试类

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.zjx.bean.BeanScope;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {

    public TestBeanScope() {
        super("classpath*:spring-beanScope.xml");
    }

    @Test
    public void test(){
        BeanScope bean = super.getBean("beanScope");
        bean.say();

        bean = super.getBean("beanScope");
        bean.say();

    }
}

控制台两次打印的哈希码一致,说明该实体类实例对象是一个;如果将XML配置文件中的scope设置为prototype,则两次得到的哈希码不一致,说明不是同一个实例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值