spring bean beanDefinition instance(scope) 的关系
2018-03-06 15:25
633 查看
原生 java class 和 object.
有了 spring 后, 在中间插了一个 bean 的概念. 一个 class 1:n bean , bean 1:n object (prototype) bean 1:1 object (singoton)
其中 一个 bean 有对应的BeanDefinition,里面含 id,scope 等信息.
参考:
1. Bean Definition从加载、解析、处理、注册到BeanFactory的过程。
2. spring 的生命周期
2. 从线上问题谈spring生命周期类lifeCycle类和bean的生命周期
3.详解Spring事件驱动模型 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型;理解它的几个关键点:
2. Spring动态创建bean
3. spring bean 的生命周期.
其他类似 BeanDefinition到Bean [没有和 spring beanFactory 的生命周期和消息结合]
http://blog.csdn.net/fei33423/article/details/52331435 4.Spring中scope singleton 和 prototype详解
4. spring mvc 的生命周期
5. 对比阅读,dubbo 的线程池,和异常处理. 事件模式.
附件,单测证明上诉实体关系:
1.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/simple-test-spring.xml" })
@TransactionConfiguration(transactionManager = "tradeTransactionManager", defaultRollback = false)
@Service
public class SimpleSpringTest implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
System.out.println(" SimpleSpringTest closed");
}
@Test
public void testThreadPoolShutDownAndBeanScope() throws InterruptedException {
SimpleSpringTest bean = ApplicationContextUtil.getBean(SimpleSpringTest.class);
bean.execute();
Thread.sleep(1000 * 60 * 3);
}
public void execute() {
final ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
PrototypeScopeBean bean = (PrototypeScopeBean) applicationContext.getBean("PrototypeScopeBeanAnnotationName");
bean.submit();
PrototypeScopeBean bean2 = (PrototypeScopeBean) applicationContext.getBean("PrototypeScopeBeanAnnotationName");
bean2.submit();
System.out.println("12");
ApplicationContextUtil.getApplicationContext().getBean(SingletonScopeBean.class);
// ③ 会抛错,org.springframework.beans.factory.NoUniqueBeanDefinitionException:
//No qualifying bean of type [com.test.SingletonScopeBean] is defined:
// expected single matching bean but found 3: SingtonScopeBeanAnnotationName,SingtonScopeBeanXmlInit2,SingtonScopeBeanXmlInit3
}
}
2.
spring 配置文件<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="tradeApplicationContext" class="com.ApplicationContextUtil"></bean>
<tx:annotation-driven/>
<context:component-scan base-package="com.test.unmock.test"/>
<context:annotation-config/>
<bean id="PrototypeScopeBeanXmlInit1" name="PrototypeScopeBean1_name"
class="com.test.PrototypeScopeBean" scope="prototype"></bean>
<bean id="PrototypeScopeBeanXmlInit2" name="PrototypeScopeBean2_name"
class="com.test.PrototypeScopeBean" scope="prototype"></bean>
<bean id="SingtonScopeBeanXmlInit2" name="SingtonScopeBeanName2"
class="com.test.SingletonScopeBean"></bean>
<bean id="SingtonScopeBeanXmlInit3" name="SingtonScopeBeanName3"
class="com.SingletonScopeBean"></bean>
</beans>
有了 spring 后, 在中间插了一个 bean 的概念. 一个 class 1:n bean , bean 1:n object (prototype) bean 1:1 object (singoton)
其中 一个 bean 有对应的BeanDefinition,里面含 id,scope 等信息.
参考:
1. Bean Definition从加载、解析、处理、注册到BeanFactory的过程。
2. spring 的生命周期
2. 从线上问题谈spring生命周期类lifeCycle类和bean的生命周期
3.详解Spring事件驱动模型 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型;理解它的几个关键点:
2. Spring动态创建bean
3. spring bean 的生命周期.
其他类似 BeanDefinition到Bean [没有和 spring beanFactory 的生命周期和消息结合]
http://blog.csdn.net/fei33423/article/details/52331435 4.Spring中scope singleton 和 prototype详解
4. spring mvc 的生命周期
5. 对比阅读,dubbo 的线程池,和异常处理. 事件模式.
附件,单测证明上诉实体关系:
1.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/simple-test-spring.xml" })
@TransactionConfiguration(transactionManager = "tradeTransactionManager", defaultRollback = false)
@Service
public class SimpleSpringTest implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
System.out.println(" SimpleSpringTest closed");
}
@Test
public void testThreadPoolShutDownAndBeanScope() throws InterruptedException {
SimpleSpringTest bean = ApplicationContextUtil.getBean(SimpleSpringTest.class);
bean.execute();
Thread.sleep(1000 * 60 * 3);
}
public void execute() {
final ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
PrototypeScopeBean bean = (PrototypeScopeBean) applicationContext.getBean("PrototypeScopeBeanAnnotationName");
bean.submit();
PrototypeScopeBean bean2 = (PrototypeScopeBean) applicationContext.getBean("PrototypeScopeBeanAnnotationName");
bean2.submit();
System.out.println("12");
ApplicationContextUtil.getApplicationContext().getBean(SingletonScopeBean.class);
// ③ 会抛错,org.springframework.beans.factory.NoUniqueBeanDefinitionException:
//No qualifying bean of type [com.test.SingletonScopeBean] is defined:
// expected single matching bean but found 3: SingtonScopeBeanAnnotationName,SingtonScopeBeanXmlInit2,SingtonScopeBeanXmlInit3
}
}
2.
import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Scope; import org.springframework.context.event.ContextClosedEvent; import org.springframework.stereotype.Service; import java.util.concurrent.atomic.AtomicInteger; /** */ @Service("SingtonScopeBeanAnnotationName") @Scope() public class SingletonScopeBean implements ApplicationListener<ContextClosedEvent> { static AtomicInteger count = new AtomicInteger(); @Override public void onApplicationEvent(ContextClosedEvent event) { System.out.println("SingletonScopeBean closed" + count.incrementAndGet()); } public void print() { System.out.println(" print " + count.get()); } }
spring 配置文件<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="tradeApplicationContext" class="com.ApplicationContextUtil"></bean>
<tx:annotation-driven/>
<context:component-scan base-package="com.test.unmock.test"/>
<context:annotation-config/>
<bean id="PrototypeScopeBeanXmlInit1" name="PrototypeScopeBean1_name"
class="com.test.PrototypeScopeBean" scope="prototype"></bean>
<bean id="PrototypeScopeBeanXmlInit2" name="PrototypeScopeBean2_name"
class="com.test.PrototypeScopeBean" scope="prototype"></bean>
<bean id="SingtonScopeBeanXmlInit2" name="SingtonScopeBeanName2"
class="com.test.SingletonScopeBean"></bean>
<bean id="SingtonScopeBeanXmlInit3" name="SingtonScopeBeanName3"
class="com.SingletonScopeBean"></bean>
</beans>
相关文章推荐
- 【spring源码分析】BeanDefinitionRegistryPostProcessor接口可自定义bean加入IOC
- org.springframework.beans.factory.BeanDefinitionStoreException: Could not resolve bean definition re
- Spring 源码阅读 BeanFactory(二) 之registerBeanDefinition方法
- Spring BeanFactory、ApplicationContext层次结构和BeanDefinition解析过程
- (spring-第6回【IoC基础篇】)BeanDefinition——实例化Bean之前的第一大利器。
- spring AbstractBeanDefinition创建bean类型是动态代理类的方式
- 一、从 BeanDefinitionParserDelegate 解析xml bean元素,查看bean的各个属性(Spring3.2.12)
- Spring图解将bean标签转换BeanDefinition
- 003-Spring4 扩展分析BeanPostProcessor、BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor
- Spring源码学习--Bean注入解析结果BeanDefinition
- Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition w
- org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'd
- 理解spring的BeanDefinition和配置文件(原文标题:动态注册bean到spring容器)
- org.springframework.beans.factory.BeanDefinitionStoreException: Unrecognized xbean element mapping: beans in namespace h
- spring解析xml中的bean的一个关键类public class BeanDefinitionParserDelegate
- Spring 3.2 源码解析 -- XML bean 元素到 BeanDefinition 解析过程
- Spring中的Bean标签的所有元素和属性BeanDefinitionParserDelegate
- 死磕Spring系列之二,bean标签的解析和BeanDefinition的注册
- Spring源码阅读 --BeanDefinition
- Spring源码之旅(3)_BeanDefinition的解析与注册