Bean的初始化和销毁

SpringBean的生命周期对的操作提供了支持,在使用JAVA配置注解配置下提供如下两种方式:

(1)Java配置方式:使用@BeaninitMethoddestoryMethod。(相当于xml配置的init-methoddestory-method

(2)注解方式:利用JSR-250@PostConstruct@PreDestory

Java配置方式演示

(1)BeanWayService

package my.spring.service;

public class BeanWayService {

    public void init() {
        System.out.println("BeanWayService 的初始化方法");
    }

    public BeanWayService() {
        System.out.println("BeanWayService 的构造方法");
    }

    public void destory() {
        System.out.println("BeanWayService 的销毁方法");
    }
}

(2)Config

package my.spring.configuration;

import my.spring.service.BeanWayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("my.spring")
public class Config {

    @Bean(initMethod = "init",destroyMethod = "destory")
    public BeanWayService beanWayService() {
        return new BeanWayService();
    }

}

(3)运行

package my.spring;

import my.spring.configuration.Config;
import my.spring.service.BeanWayService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        // new一个AnnotationConfigApplicationContext对象context
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        // 从容器中获得对象
        BeanWayService beanWayService = context.getBean(BeanWayService.class);

        context.close();
    }
}

运行结果:



注解方式演示

(1)添加JSR250依赖

<!-- JSR250依赖-->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>jsr250-api</artifactId>
    <version>1.0</version>
</dependency>

(2)JSR250WayService

package my.spring.service;

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

public class JSR250WayService {

    @PostConstruct
    public void init() {
        System.out.println("JSR250WayService 的初始化方法");
    }

    public JSR250WayService() {
        System.out.println("JSR250WayService 的构造方法");
    }

    @PreDestroy
    public void destory() {
        System.out.println("JSR250WayService 的销毁方法");
    }

}

(3)Config

package my.spring.configuration;

import my.spring.service.JSR250WayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("my.spring")
public class Config {
    
    @Bean(initMethod = "init",destroyMethod = "destory")
    public JSR250WayService jsr250WayService() {
        return new JSR250WayService();
    }
}

(4)运行

package my.spring;

import my.spring.configuration.Config;
import my.spring.service.JSR250WayService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        // new一个AnnotationConfigApplicationContext对象context
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        // 从容器中获得对象
        JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);

        context.close();
    }
}

运行结果:



总结

initMethod指定BeanWayService类的init方法在构造方法之后执行,

  destoryMethod指定BeanWayService类的destory方法在Bean销毁之前执行。

@PostConstruct,在构造方法执行完执行之后执行,

  @PreDestory,在Bean销毁之前执行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值