spring启动加载程序的几种方法
2018-02-28 23:43
274 查看
http://blog.csdn.net/aa2484219/article/details/51261227
转载自:http://blog.csdn.net/lovesomnus/article/details/50158761
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
[java] view plain copyimport javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class DataInitializer{
@PostConstruct
public void initMethod() throws Exception {
System.out.println("initMethod 被执行");
}
@PreDestroy
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被执行");
}
}
第二种是:通过 在xml中定义init-method 和 destory-method方法[java] view plain copypublic class DataInitializer{
public void initMethod() throws Exception {
System.out.println("initMethod 被执行");
}
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被执行");
}
}
[html] view plain copy<bean id="dataInitializer" class="com.somnus.demo.DataInitializer" init-method="initMethod" destory-method="destroyMethod"/>
第三种是: 通过bean实现InitializingBean和 DisposableBean接口[java] view plain copyimport org.springframework.beans.factory.DisposableBean;
public class DataInitializer implements InitializingBean,DisposableBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet 被执行");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy 被执行");
}
}
其中第一种和第二种是同一种形式,只不过一种xml配置,另外一种采用注解形式罢了,有很大区别的是第三种,如果同一个bean同时采用两种方式初始化的时候执行某个方法,首先在执行顺序上就会体现出来。先执行afterPropertiesSet(),后执行initMethod()
这里我们看下源码这方式在spring中是怎么实现的?通过查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出其中奥妙AbstractAutowireCapableBeanFactory类中的invokeInitMethods讲解的非常清楚,源码如下:
[java] view plain copyprotected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable {
//判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,则只掉调用bean的afterPropertiesSet方法
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
//直接调用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
return null;
}
},getAccessControlContext());
} catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//直接调用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
}
}
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
//判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
//进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
invokeCustomInitMethod(beanName, bean, mbd);
}
}
总结:
1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用
2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖
3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。
转载自:http://blog.csdn.net/lovesomnus/article/details/50158761
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
[java] view plain copyimport javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class DataInitializer{
@PostConstruct
public void initMethod() throws Exception {
System.out.println("initMethod 被执行");
}
@PreDestroy
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被执行");
}
}
第二种是:通过 在xml中定义init-method 和 destory-method方法[java] view plain copypublic class DataInitializer{
public void initMethod() throws Exception {
System.out.println("initMethod 被执行");
}
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被执行");
}
}
[html] view plain copy<bean id="dataInitializer" class="com.somnus.demo.DataInitializer" init-method="initMethod" destory-method="destroyMethod"/>
第三种是: 通过bean实现InitializingBean和 DisposableBean接口[java] view plain copyimport org.springframework.beans.factory.DisposableBean;
public class DataInitializer implements InitializingBean,DisposableBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet 被执行");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy 被执行");
}
}
其中第一种和第二种是同一种形式,只不过一种xml配置,另外一种采用注解形式罢了,有很大区别的是第三种,如果同一个bean同时采用两种方式初始化的时候执行某个方法,首先在执行顺序上就会体现出来。先执行afterPropertiesSet(),后执行initMethod()
这里我们看下源码这方式在spring中是怎么实现的?通过查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出其中奥妙AbstractAutowireCapableBeanFactory类中的invokeInitMethods讲解的非常清楚,源码如下:
[java] view plain copyprotected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable {
//判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,则只掉调用bean的afterPropertiesSet方法
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
//直接调用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
return null;
}
},getAccessControlContext());
} catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//直接调用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
}
}
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
//判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
//进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
invokeCustomInitMethod(beanName, bean, mbd);
}
}
总结:
1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用
2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖
3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。
相关文章推荐
- spring启动加载程序的几种方法介绍
- spring启动加载程序的几种方法
- spring启动加载程序的几种方法
- spring应用启动加载自定义类方法的几种方式
- C#启动外部程序以及等待外部程序关闭的几种方法
- C#启动外部程序的几种方法
- C# 启动外部程序的几种方法
- 简介下程序的启动加载和调用的方法Application
- C# 启动外部程序的几种方法
- C# 启动外部程序的几种方法
- C# 启动外部程序的几种方法(转自:http://www.cnblogs.com/Slxj/archive/2012/01/12/2320531.html)
- C# 启动外部程序的几种方法
- SpringBoot实践之---系统启动加载实现的几种方法init、servelt、CommandLineRunner
- C# 启动外部程序的几种方法
- C#启动外部程序的几种方法以及等待外部程序关闭的方法
- 程序只启动一个实例的几种方法
- C# 启动外部程序的几种方法
- C# 启动外部程序的几种方法
- spring 容器启动后加载某个方法
- C# 启动外部程序的几种方法