Spring入门篇 学习笔记
添加 BeanScope:
public class BeanScope {
public void say() {
System.out.println("BeanScope say : " + this.hashCode());
}
}
添加配置文件 spring-beanscope-singleton.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="beanScope" class="com.karonda.bean.BeanScope" scope="singleton"></bean>
</beans>
添加测试 TestBeanScopeSingleton:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopeSingleton extends UnitTestBase {
public TestBeanScopeSingleton() {
super("classpath*:spring-beanscope-singleton.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
}
添加配置文件 spring-beanscope-prototype.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="beanScope" class="com.karonda.bean.BeanScope" scope="prototype"></bean>
</beans>
添加测试 TestBeanScopePrototype:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopePrototype extends UnitTestBase {
public TestBeanScopePrototype() {
super("classpath*:spring-beanscope-prototype.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
beanScope = super.getBean("beanScope");
beanScope.say();
}
}
原文:https://www.cnblogs.com/victorbu/p/10415863.html