原生 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事件驱动模型 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型;理解它的几个关键点:
其他类似 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>