怎么实现bean初始化摧毁方法的注入

发布时间:2023-04-28 11:26:45 作者:iii
来源:亿速云 阅读:102

本文小编为大家详细介绍“怎么实现bean初始化摧毁方法的注入”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么实现bean初始化摧毁方法的注入”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实现 bean 初始化、摧毁方法的配置与处理

spring支持我们自定义 bean 的初始化方法和摧毁方法。配置方式可以通过 xml 的 init-methoddestory-method配置,或者实现 InitializingBeanDisposableBean接口,来完成自定义的初始化和bean的销毁。 在项目开发过程中,相信最多看到的是 @PostConstruct 注解标识的方法来进行bean的初始化。

@PostConstruct 是 Spring Framework 提供的注解,可以用于在 Bean 实例化之后执行初始化操作

通过xml配置定义初始化、摧毁方法

BeanDefinition 里面添加 initMethodName、和 destoryMethodName 属性,来记录通过配置注入的初始化和摧毁方法名称。然后在解析 xml 文件的 cn.anoxia.springframework.beans.factory.xml.XmlBeanDefinitionReader#doLoadBeanDefinitions方法中,完成 属性的注入。

protected void doLoadBeanDefinitions(InputStream inputStream) throws Exception {
        Document doc = XmlUtil.readXML(inputStream);
        Element root = doc.getDocumentElement();
        NodeList childNodes = root.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            ....
            String initMethod = bean.getAttribute("init-method");
            String destroyMethod = bean.getAttribute("destroy-method");
            Class<?> clazz = Class.forName(calssName);
            String beanName = StrUtil.isNotEmpty(id) ? id : name;
            if (StrUtil.isEmpty(beanName)){
                beanName = StrUtil.lowerFirst(clazz.getSimpleName());
            }
            // 定义bean
            BeanDefinition beanDefinition = new BeanDefinition(clazz);
            // 设置初始化、摧毁方法
            beanDefinition.setInitMethodName(initMethod);
            beanDefinition.setDestoryMethodName(destroyMethod);
            ....
        }
    }

通过实现接口

实现 InitializingBean,DisposableBean 并实现里面的方法,来自定义bean的初始化和摧毁方法

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
public interface DisposableBean {
    void destroy() throws Exception;
}

在 创建bean的过程中,完成方法的注入,区分xml配置与接口实现。

initMethod 方法的注入与执行

@Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
        Object bean = null;
        try {
            bean = createBeanInstance(beanDefinition, beanName, args);
            // 注入属性
            applyPropertyValues(beanName, bean, beanDefinition);
            // 提供给外部的扩展包装,执行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置处理方法
            bean = initializeBean(beanName, bean, beanDefinition);
        } catch (Exception e) {
            throw new RuntimeException("bean create error!", e);
        }
        // 注册实现了 DisposableBean 接口的 Bean 对象
        registerDisposableBeanIfNecessary(beanName, bean, beanDefinition);
        registerSingleton(beanName, bean);
        return bean;
    }
private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) throws BeansException {
        // 1. 执行 BeanPostProcess before 操作
        Object wrappedBean = applyBeanPostProcessorsBeforeInitialization(bean, beanName);
        try {
            // 执行bean初始化方法
            invokeInitMethods(beanName,wrappedBean,beanDefinition);
        }catch (Exception e){
            throw new BeansException("Invocation of init method of bean[" + beanName + "] failed", e);
        }
        // 2. 执行 BeanPostProcess after 操作
        wrappedBean = applyBeanPostProcessorsAfterInitialization(bean,beanName);
        return wrappedBean;
    }
private void invokeInitMethods(String beanName, Object bean, BeanDefinition beanDefinition) throws Exception {
    // 如果是通过接口实现,直接使用接口提供的方法    
    if (bean instanceof InitializingBean) {
            ((InitializingBean) bean).afterPropertiesSet();
        }
    // 通过xml配置,获取方法执行
        String initMethodName = beanDefinition.getInitMethodName();
        if (StrUtil.isNotEmpty(initMethodName)) {
            Method method = beanDefinition.getBeanClass().getMethod(initMethodName);
            // getMethod 已经做了非空判断
            if (null == method) {
                throw new BeansException("Could not find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'");
            }
            method.invoke(bean);
        }
    }

destroyMethod 方法的注入与执行

提供一个适配器、来完成xml和接口的适配处理。处理逻辑基本与 init方法相似

/**
 * bean 摧毁适配器
 * @author huangle
 * @date 2023/3/7 10:26
 */
public class DisposableBeanAdapter implements DisposableBean {
    /**
     * bean名字
     */
    private final String beanName;
    /**
     * bean
     */
    private final Object bean;
    /**
     * 销毁方法名称
     */
    private String destroyMethodName;
    public DisposableBeanAdapter(String beanName, Object bean, BeanDefinition beanDefinition) {
        this.beanName = beanName;
        this.bean = bean;
        this.destroyMethodName = beanDefinition.getDestoryMethodName();
    }
    @Override
    public void destroy() throws Exception {
        // 1. 实现 DisposableBean 接口,完成摧毁扩展
        if (bean instanceof DisposableBean) {
            ((DisposableBean) bean).destroy();
        }
        // 2. 通过xml配置 配置 destroy 方法 实现
        if (StrUtil.isNotEmpty(destroyMethodName) &amp;&amp; !(bean instanceof DisposableBean &amp;&amp; "destory".equals(destroyMethodName))) {
            Method destroyMethod = bean.getClass().getMethod(destroyMethodName);
            if (null == destroyMethod) {
                throw new BeansException("Couldn't find a destroy method named '" + destroyMethodName + "' on bean with name '" + beanName + "'");
            }
            destroyMethod.invoke(bean);
        }
    }
}

测试

userDao 通过xml配置初始化和摧毁方法,userService 通过继承接口来实现方法。

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="userDao" class="cn.anoxia.springframework.beans.factory.support.UserDao" init-method="initMethod" destroy-method="destroyMethod"/>
    <bean id="userService" class="cn.anoxia.springframework.beans.factory.support.UserService">
        <property name="name" value="Anoxia"/>
        <property name="nickname" value="迪迦"/>
        <property name="userDao" ref="userDao"/>
    </bean>
<!--    <bean id="myBeanPostProcecssor" class="cn.anoxia.springframework.beans.factory.support.MyBeanPostProcecssor"/>-->
<!--    <bean id="myFactoryPostProcessor" class="cn.anoxia.springframework.beans.factory.support.MyBeanFactoryPostProcessor"/>-->
</beans>
public class UserService implements InitializingBean, DisposableBean{
    @Override
    public void destroy() throws Exception {
        System.out.println("userService执行:destroy 方法");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("userService执行:init 方法");
    }
}

测试结果

怎么实现bean初始化摧毁方法的注入

读到这里,这篇“怎么实现bean初始化摧毁方法的注入”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Spring容器中添加bean的方式有哪些
  2. zuulFilter中注入bean失败如何解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

bean

上一篇:SpringBoot依赖管理源码分析

下一篇:spring初始化源码之关键类和扩展接口怎么应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》