2021-09-29

Spring Bean详解(二)


前言

Spring 生命周期的意义就在于,可以利用 Bean 在其存活期间的指定时刻完成一些相关操作。一般情况下,会在 Bean 被初始化后和被销毁前执行一些相关操作。

Spring 官方提供了 3 种方法实现初始化回调和销毁回调:
实现 InitializingBean 和 DisposableBean 接口;
在 XML 中配置 init-method 和 destory-method;
使用 @PostConstruct 和 @PreDestory 注解。


一、初始化回调

1.使用接口

<bean id="..." class="..." />
public class User implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用接口:InitializingBean,方法:afterPropertiesSet,无参数");
    }
}

2.使用XML

<bean id="..." class="..." init-method="init"/>
public class User {
    public void init() {
        System.out.println("调用init-method指定的初始化方法:init" );
    }
}

3.使用注解

public class ExampleBean {
    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct注解指定的初始化方法:init" );
    }
}

二、销毁回调

1.使用接口

<bean id="..." class="..." />
public class User implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用接口:InitializingBean,方法:afterPropertiesSet,无参数");
    }
}

2.使用XML

<bean id="..." class="..." destroy-method="destroy"/>
public class User {
    public void destroy() {
        System.out.println("调用destroy-method指定的销毁方法:destroy" );
    }
}

3.使用注解

public class ExampleBean {
    @PreDestory 
    public void destroy() {
        System.out.println("@PreDestory注解指定的初始化方法:destroy" );
    }
}

案例

1.创建HelloWorld 类

public class HelloWorld {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public void getMessage() {
        System.out.println("message : " + message);
    }
    public void init() {
        System.out.println("Bean正在进行初始化");
    }
    public void destroy() {
        System.out.println("Bean将要被销毁");
    }
}

2.创建MainApp 类

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.registerShutdownHook();
        //AbstractApplicationContext 类的 registerShutdownHook() 方法,来确保正常关机并调用相关的 destroy() 方法
    }
}

3.Beans.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="helloWorld" class="net.biancheng.HelloWorld"
        init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

运行结果如下:

Bean正在进行初始化
message : Hello WorldBean将要被销毁
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值