Spring Bean的作用域测试


基础测试类UnitTestBase:
package gdufe.injectionDao.test;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;


	public class UnitTestBase {
		
		private ClassPathXmlApplicationContext context;
		
		private String springXmlpath;
		
		public UnitTestBase() {}
		
		public UnitTestBase(String springXmlpath) {
			this.springXmlpath = springXmlpath;
		}
		
		@Before
		public void before() {
			if (StringUtils.isEmpty(springXmlpath)) {
				springXmlpath = "classpath*:spring-*.xml";
			}
			try {
				context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
				context.start();
			} catch (BeansException e) {
				e.printStackTrace();
			}
		}
		
		@After
		public void after() {
			context.destroy();
		}
		
		@SuppressWarnings("unchecked")
		protected <T extends Object> T getBean(String beanId) {
			try {
				return (T)context.getBean(beanId);
			} catch (BeansException e) {
				e.printStackTrace();
				return null;
			}
		}
		
		protected <T extends Object> T getBean(Class<T> clazz) {
			try {
				return context.getBean(clazz);
			} catch (BeansException e) {
				e.printStackTrace();
				return null;
			}
		}

	}


BeanScope:
在hashCode()方法中,我们可以根据输出判断是不是同一个类
package gdufe.injection.bean;

public class BeanScope {
	public void say(){
		System.out.println("结果:"+this.hashCode());
	}
}
测试类:TestBeanScope:
package gdufe.injectionDao.test;

import gdufe.injection.bean.BeanScope;

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

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
	public TestBeanScope(){
		super("classpath*:spring-beanscope.xml");
	}
@Test
	public void testSay(){
		BeanScope beanScope=super.getBean("beanScope");
		beanScope.say();
		
		BeanScope beanScope1=super.getBean("beanScope");
		beanScope1.say();
	}
}


当scope=“singleton”,spring-bean.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
 	<bean id="beanScope" class="gdufe.injection.bean.BeanScope" scope="singleton">
 	
 		
 	</bean>
        
        </beans>
输出结果:
结果:1864350231
结果:1864350231 可以看出是同一个类
当scope="prototype"
输出结果:
结果:1668627309
结果:1795799895 不同的类

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值