Spring(5)-Bean的初始化跟销毁

13 篇文章 0 订阅
1.简要说明

在我们实际开发的时候,经常会遇到在Bean在使用之前或者之后做些必要的操作,Spring
对Bean的生命周期的操作提供了支持。在使用Java配置和注解配置下提供如下两种方式:
(1) Java 配置方式:使用@Bean的initMethod 和destroyMethod (相当于xml 配置的
init-method和destory-method)。
(2)注解方式:利用JSR-250的@PostConstruct和@PreDestroy。

2.实战演示(导入依赖)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lglg</groupId>
    <artifactId>spring-demo01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.annotation/jsr250-api -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
3.编写相关Bean
package com.lglg.springdemo01;

/**
 * Date:2020/8/15
 * 使用@Bean 形式
 * @author:lg
 */
public class BeanWayService {

    public void init(){
        System.out.println("@Bean-init-method");
    }

    public BeanWayService(){
        super();
        System.out.println("初始化构造函数-BeanWayService");

    }

    public void destroy(){
        System.out.println("@bean-destory-method");
    }
}

package com.lglg.springdemo01;

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

/**
 * Date:2020/8/15
 *
 * @author:lg
 */
public class JSR250WayService {

    // 在构造函数执行之后执行
    @PostConstruct
    public void init(){
        System.out.println("jsr250-init-method");
    }

    public JSR250WayService(){
        super();
        System.out.println("初始化构造函数-JSR250WayService");
    }

    // 在Bean销毁之前执行
    @PreDestroy
    public void destroy(){
        System.out.println("jsr250-destroy-method");
    }
}

4.编写配置类
package com.lglg.config;

import com.lglg.springdemo01.BeanWayService;
import com.lglg.springdemo01.JSR250WayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Date:2020/8/15
 *
 * @author:lg
 */
@Configuration
@ComponentScan("com.lglg")
public class PrePostConfig {

    /**
     * ①initMethod和destroyMethod指定BeanWayService 类的init和destroy方法在构造之后、
     Bean销毁之前执行。

     * @return
     */
    @Bean(initMethod = "init",destroyMethod = "destroy")
    BeanWayService beanWayService(){
        return new BeanWayService();
    }

    @Bean
    JSR250WayService jsr250WayService(){
        return new JSR250WayService();
    }
}


5.编写测试类
package com.lglg.demoTest;

import com.lglg.config.PrePostConfig;
import com.lglg.springdemo01.BeanWayService;
import com.lglg.springdemo01.JSR250WayService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Date:2020/8/15
 *
 * @author:lg
 */
public class Demo05 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class);

        BeanWayService bean = context.getBean(BeanWayService.class);
        JSR250WayService bean1 = context.getBean(JSR250WayService.class);
        context.close();
    }
}

6.测试结果

初始化构造函数-BeanWayService
@Bean-init-method
初始化构造函数-JSR250WayService
jsr250-init-method
jsr250-destroy-method
@bean-destory-method

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值