Spring Context和Bean Lifecycle回调:用法的实际示例 [英] Spring Context and Bean Lifecycle callbacks: practical examples of usage

查看:81
本文介绍了Spring Context和Bean Lifecycle回调:用法的实际示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring有一点经验.我想知道Spring Context/Bean生命周期中的回调数量.我从没使用过它们,并且可以想象其中最需要它们的情况.

I've a little experience in Spring. And I wondering about the amount of callbacks in Spring Context/Bean Lifecycle. I've never used them, and can imaging situations in which the most of them are needed.

我的问题是 :您可以为每个回调提供至少一个用法示例吗?表示需要该回调的情况.

My question is: can you provide for each callback at least one example of usage? Means situations when you need that callback.

扩展回调:

Bean回调:

P.S.:

P.S.:

对于我来说很清楚何时调用了大多数回调,或者为ApplicationContext编写了哪种或另一种实现.但是我无法弄清楚为什么有人可能希望从该回调\实现中获利.例如:

It is clear for me when majority of callbacks are calling, or what one or another implementation of ApplicationContext was written for. But I can't figure out why someone may want to profit from that callback\implementation. For example:

  • AbstractRefreshableApplicationContext用于动态更改Bean配置.但为什么?在哪种情况下我可能想即时更改bean的配置?
  • afterPropertiesSet回调,显然是在设置了所有bean的属性之后调用的:)但是为什么我应该知道这一点以及何时(可能想要)使用它?
  • AbstractRefreshableApplicationContext is using to change bean configurations on fly. But Why? In which situation I may want to change bean's configuration on fly?
  • afterPropertiesSet callback, obviously is invoked after all bean's properties are setted :) But why I should know about that, and when I should (may want) use it?

推荐答案

您可以为每个回调提供至少一个用法示例吗?

can you provide for each callback at least one example of usage?

查看每个接口的javadoc,检查任何实现类的目的,并查看其实现源代码.

Look at the javadoc for each of the interfaces, check any of the implementing classes for their purpose and look up their source code for their implementation.

典型的bean定义是

<bean id="someBean" class="com.example.beans.SomeBean">
    <property name="someProperty" value="42" />
    <property name="other" value="I will always love you." />
</bean>

具有类似的类

public class SomeBean {
    private String someProperty;
    private String other;
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
    public void setOther(String other) {
        this.other = other;
    }
}

但是有些时候,有些类需要根据属性集执行一些逻辑

But some times you have classes where you need to perform some logic based on the properties set

public class SomeBean {
    private String someProperty;
    private String other;
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public void init() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                // for example
                // send the two property values to some external service
            }
        });
        thread.start();
    }
}

仅在设置属性后才能执行此逻辑.在这种情况下,您可以让您的班级实现InitializingBean接口(旧学校)

This logic can only be performed after the properties have been set. In that case, you can have your class implement the InitializingBean interface (old school)

public class SomeBean implements InitializingBean {
    private String someProperty;
    private String other;
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public void init() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                // for example
                // send the two property values to some external service
            }
        });
        thread.start();
    }
    public void afterPropertiesSet() throws Exception {
        init();
    }
}

或使用@PostConstruct(新学校)对其进行注释

Or annotate it with @PostConstruct (new school)

public class SomeBean implements InitializingBean {
    private String someProperty;
    private String other;
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
    public void setOther(String other) {
        this.other = other;
    }
    @PostConstruct
    public void init() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                // for example
                // send the two property values to some external service
            }
        });
        thread.start();
    }
}

这只是一个例子. InitializingBean接口通常与FactoryBean接口一起使用.它有助于在生成对象之前初始化工厂.有关更多示例,请参见这两个接口的javadoc并查找各种实现类的源代码.对其他*Aware接口执行相同的操作.

This is just an example. The InitializingBean interface is often used along with the FactoryBean interface. It helps to initialize the factory before it produces an object. For more example, see the javadoc of both of those interfaces and look up the source code of the various implementing classes. Do the same for the other *Aware interfaces.

对于AbstractRefreshableApplicationContext,有时您需要refresh()您的ApplicationContext.之所以会发生这种情况,是因为您想重新加载XML配置,或者因为您的环境已更改,但又不想停止/重新启动该应用程序.

As for AbstractRefreshableApplicationContext, some times you need to refresh() your ApplicationContext. This can happen because you want to reload an XML configuration or because your environment has changed, but you don't want to stop/re-start the application.

这篇关于Spring Context和Bean Lifecycle回调:用法的实际示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆