栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring的生命周期回调

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring的生命周期回调

一、spring的初始化:
(1)实现spring内部接口InitializingBean并且重写方法afterPropertiesSet()。
实例:
LifeCallInit的bean对象:

package com.it.app.lifecycle;
import org.springframework.beans.factory.InitializingBean;

public class LifeCallInit implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行方法:afterPropertiesSet");
    }
}
package com.it.app.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.importResource;

@ComponentScan("com")
@importResource("spring-contextFive.xml")
public class AppConfig {
}

xml配置文件:



    
    
   

执行测试类:

package com.it.app;

import com.it.app.config.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AppSix {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        
    }
}

测试结果:

执行方法:afterPropertiesSet

(2)通过注解@PostConstruct
修改LifeCallInit的bean对象:

package com.it.app.lifecycle;

import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;

public class LifeCallInit implements InitializingBean {

   @PostConstruct
    public void postConstruct(){
       System.out.println("执行方法:postConstruct");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行方法:afterPropertiesSet");

    }
}
package com.it.app;

import com.it.app.config.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AppSix {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

    }

}

执行结果:

执行方法:postConstruct
执行方法:afterPropertiesSet

(3)通过xml标签bean里面的init-method="xxx"标签(也可以用@Bean注解的initMethod 标签 )
修改LifeCallInit的bean对象:

package com.it.app.lifecycle;

import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;

public class LifeCallInit implements InitializingBean {
    
    public void init(){
        System.out.println("执行方法:init");
    }
    
   @PostConstruct
    public void postConstruct(){
       System.out.println("执行方法:postConstruct");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行方法:afterPropertiesSet");

    }
}

修改xml文件:init-method="init"的方法是LifeCallInit的bean对象里面的init方法。




    

执行结果(当以上三种情况都使用的情况下):
执行顺序是:
1、@PostConstruct
2、 InitializingBean 的afterPropertiesSet() 方法
3、init-method

执行方法:postConstruct
执行方法:afterPropertiesSet
执行方法:init

二、spring的销毁
(1)spring内部DisposableBean接口实现方法destroy
例子:
LifeCallDestroy的bean类

package com.it.app.lifecycle;

import org.springframework.beans.factory.DisposableBean;

public class LifeCallDestroy implements DisposableBean {

    @Override
    public void destroy() throws Exception {
        System.out.println("执行方法:destroy");
    }
}
package com.it.app.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.importResource;

@ComponentScan("com")
@importResource("spring-contextFive.xml")
public class AppConfig {
}

xml文件:




    

执行main方法:

package com.it.app;

import com.it.app.config.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AppSix {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }

}

执行结果:

执行方法:destroy

(2)通过注解@PreDestroy
修改LifeCallDestroy的bean类

package com.it.app.lifecycle;

import org.springframework.beans.factory.DisposableBean;

import javax.annotation.PreDestroy;

public class LifeCallDestroy implements DisposableBean {


    @PreDestroy
    public void  preDestroy(){
        System.out.println("执行方法:preDestroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("执行方法:destroy");
    }
}

测试结果:

执行方法:preDestroy
执行方法:destroy

(3)xml配置文件的bean标签的destroy-method
修改LifeCallDestroy:

package com.it.app.lifecycle;

import org.springframework.beans.factory.DisposableBean;

import javax.annotation.PreDestroy;

public class LifeCallDestroy implements DisposableBean {


    public void destroyMethod(){
        System.out.println("执行方法:destroyMethod");
    }


    @PreDestroy
    public void  preDestroy(){
        System.out.println("执行方法:preDestroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("执行方法:destroy");
    }
}

修改xml文件:




    

执行结果:
三种销毁方法同时存在的执行顺序:
1、@PreDestroy
2、DisposableBean的接口实现方法destroy()
3、bean标签destroy-method

执行方法:preDestroy
执行方法:destroy
执行方法:destroyMethod
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号