`

spring的init-method和destroy-method方法使用

阅读更多
http://static.springsource.org/spring/docs/2.0.x/reference/beans.html
3.5.1.2. Destruction callbacks
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="$[]" />
</bean>
destroy-method指定了当要销毁bean datasource之前要做的操作,也就是这个bean的收尾工作。
这里是指定了close()方法。
Closes and releases all idle connections that are currently stored in the connection pool associated with this data source.
http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
也可以像下面这样设置全局的init方法
<beans default-init-method="init">
    <bean id="blogService" class="com.foo.DefaultBlogService">
        <property name="blogDao" ref="blogDao" />
    </bean>
</beans>

要注意的是init和interceptor一起使用的情况,当bean和intereptor分开定义时,可以绕过proxy访问这个bean了。但是,还是不要在init里关联interceptor,因为
这样bean的生命周期会与proxy/interceptors耦合:
Finally, please be aware that the Spring container guarantees that a configured initialization callback is called immediately after a bean has been supplied with all of it's dependencies. This means that the initialization callback will be called on the raw bean reference, which means that any AOP interceptors or suchlike that will ultimately be applied to the bean will not yet be in place. A target bean is fully created first, then an AOP proxy (for example) with its interceptor chain is applied. Note that, if the target bean and the proxy are defined separately, your code can even interact to the raw target bean, bypassing the proxy. Hence, it would be very inconsistent to apply the interceptors to the init method, since that would couple the lifecycle of the target bean with its proxy/interceptors, and leave strange semantics when talking to the raw target bean directly.
分享到:
评论

相关推荐

    08-IoC配置-bean的生命周期控制

    Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期...在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。

    Spring IOC Bean标签属性介绍(教学视频+源代码)

    Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml ...1.9 init-method和destroy-method 1.9.1 实体类JavaBean User加自定义的初始化方法和销毁方法 1.9.3 加了lazy

    Spring框架中 @Autowired 和 @Resource 注解的区别

     为了定义一个 bean 的安装和卸载,我们可以使用 init-method 和 destroy-method 参数简单的声明一下 ,其中 init-method 属性指定了一个方法,该方法在 bean 的实例化阶段会立即被调用;同样地,destro

    spring 配置文件简单说明

    2)default-destroy-method="方法名" 定义在此配置文件中的bean都会在执行指定的destroy方法。 3)default-lazy-init ="false|true" 定义在此配置文件中的bean都会延迟加载。 ....................

    EWALectureNotes:幻灯片和讲座笔记Hacettepe大学的企业Web体系结构讲座的笔记

    EWA讲义 Hacettepe大学企业Web体系结构讲座的幻灯片和示例应用程序 应用清单: AjaxApp:基于Eclipse的Web项目,使用jQuery演示...spring-bean-lifecycle:使用init-method,destroy-method,InitializingBean,Disp

    spring1.2学习心得分享

    资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    07-IoC配置-scope属性

    scope属性 名称: scope类型:属性 归属: bean标签 作用:定义bean的作用范围 格式: “singleton"&gt; ...singleton:设定创建出的对象保存在spring容器中,是一个单例的对象(bean默认是...名称: init-method, destroy-metho

    spring1.1开发理解

    资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    spring学习心得

    资源释放:&lt;bean destroy-method=""/&gt;仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    spring-xmemcached

    &lt;bean id="cachingClient" class="com.dmx.cache.caching.impl.CachingXMemcachedClient" init-method="init" destroy-method="destroy"&gt; ${XMemcached_isflushAll}" /&gt; ...

    Spring.html

    注意:使用注解的方式,最终通知和后置通知顺序换了,建议使用环绕通知 注解 配置 声明式事务管理 PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:...

    Spring的学习笔记

    (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier ...

    xmljava系统源码-springdemo:spring3的IOC,AOP,JDBC,orm等各个模块的使用示例集锦,可以作为入门的spri

    xml java系统源码 springdemo spring3的IOC,AOP,JDBC,orm等各个模块的使用示例集锦,可以作为入门...init-method,destroy-method,initiableBean, DisposableBean,lasyinit,singleton,prototype @PostConstruct @P

    spring2.5 学习笔记

    (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier ...

    JSP Spring中Druid连接池配置详解

    JSP Spring中Druid连接池配置 jdbc.properties url=jdbc:postgresql://***.... &lt;bean id=dataSource class=com.alibaba.druid.pool.DruidDataSource init-method=init destroy-method=close&gt; &lt;!-- 基本属性 url、u

    Spring bean初始化及销毁你必须要掌握的回调方法.docx

    3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...

    Spring MVC 入门实例

    7 &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; 8 ${jdbc.driverClassName}"/&gt; 9 ${jdbc.url}"/&gt; 10 ${jdbc.username}"/&gt; 11 ${jdbc.password}"/&gt; 12 13 --...

    spring.net中文手册在线版

    4.5.1.1.IInitializingObject接口和init-method属性 4.5.1.2.IDisposable接口和destroy-method属性 4.5.2.让对象了解自己的容器 4.5.2.1.IObjectFactoryAware接口 4.5.2.2.IObjectNameAware接口 4.5.3....

    springmvcmybatis

    &lt;bean id="writeDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init" lazy-init="true"&gt; ${jdbc.driver}" /&gt; ${jdbc.url}" /&gt; ${jdbc.username}" /&gt; $...

    SpringMVC-SSH全注解

    destroy-method="close"&gt; ${jdbc.driver}" /&gt; ${jdbc.url}" /&gt; ${jdbc.username}" /&gt; ${jdbc.password}" /&gt; &lt;!-- 连接池启动时的初始值 --&gt; &lt;property name="initialSize" value="1"/&gt; ...

Global site tag (gtag.js) - Google Analytics