spring bean的初始化方式(init,destroy)

博客介绍了Spring容器中Bean的三种初始化方式。一是使用@PostConstruct和@PreDestroy注解,前者在Servlet构造后、初始化前执行;二是在@Bean中指定initMethod和destroyMethod方法;三是实现InitializingBean和DisposableBean接口,重写相关方法。还证明了注解方法执行顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring容器中bean的初始化方式大体有三种:

@PostConstruct ,@PreDestroy:  从Java EE5规范开始,Servlet中增加了两个影响Servlet生命周期的注解,@PostConstruct和@PreDestroy。@PostConstruct会在Servlet构造函数之后,初始化之前执行

package com.edu.bean;


import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
@Component
public class Color {

    @PostConstruct
    public void init(){
        System.out.println("init......");

    }

    @PreDestroy
    public void destroy(){
        System.out.println("destroy.........");

    }


}

package com.edu.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
public class Test1 {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.edu.bean");
        context.close();

    }
}

输出:

init......

destroy.........

  • @Bean中指定initMethod,destroyMethod方法
package com.edu.bean;


import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */

public class Color {

    @PostConstruct
    public void init(){
        System.out.println("init......");

    }

    public void init1(){
        System.out.println("init1......");

    }

    @PreDestroy
    public void destroy(){
        System.out.println("destroy.........");

    }

    public void destroy1(){
        System.out.println("destroy1.........");

    }


}


package com.edu.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
@Configuration
class Config {

    @Bean(name = "color",initMethod = "init1",destroyMethod = "destroy1")
    public  Color getColor(){

        return new Color();
    }

}


package com.edu.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
public class Test1 {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        context.close();

    }
}

输出:

init......
init1......

destroy.........
destroy1.........

证明@PostConstruct 修饰的方法是在initMethod标注的方法之前执行,@PreDestroy修饰的方法是在destroyMethod 标注的方法之前执行

 

  • interface InitializingBean,DisposableBean:重写afterPropertiesSet,destroy方法
package com.edu.bean;


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
@Component
public class Color implements InitializingBean, DisposableBean {

    @PostConstruct
    public void init(){
        System.out.println("init......");

    }
    @PreDestroy
    public void destroy(){
        System.out.println("destroy.........");

    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("init1.........");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy1......");
    }
}


package com.edu.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: chenzhubing
 * Date: 2019/6/17
 */
public class Test1 {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.edu.bean");
        context.close();

    }
}

输出:

init......
init1.........

destroy.........
destroy1......

 

转载于:https://www.cnblogs.com/chenzhubing/p/11041360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值