Spring Context和Bean Lifecycle回调:实际的使用示例

VB_*_*VB_ 6 java spring

我在春天有一点经验.我想知道Spring Context/Bean Lifecycle中的回调量.我从来没有使用它们,并且可以成像需要大部分的情况.

我的问题是:你能为每个回调至少提供一个使用示例吗?表示需要回调时的情况.

Conext回调: 在此输入图像描述

Bean回调: 在此输入图像描述

PS:

当大多数回调调用时,或者为ApplicationContext编写了一个或另一个实现时,我很清楚.但我无法弄清楚为什么有人可能想要从回调\实施中获利.例如:

  • AbstractRefreshableApplicationContext用于在飞行中更改bean配置.但为什么?在哪种情况下我可能想要在飞行中更改bean的配置?
  • afterPropertiesSet 回调,显然是在设置了所有bean的属性后调用的:)但为什么我应该知道这个,什么时候我应该(可能想要)使用它?

Sot*_*lis 7

您能为每个回调提供至少一个使用示例吗?

查看每个接口的 javadoc,检查任何实现类的用途,并查找其实现的源代码。

典型的 bean 定义是

<bean id="someBean" class="com.example.beans.SomeBean">
    <property name="someProperty" value="42" />
    <property name="other" value="I will always love you." />
</bean>
Run Code Online (Sandbox Code Playgroud)

像这样的课程

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但有时您的类需要根据属性集执行一些逻辑

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

该逻辑只能在设置属性后执行。在这种情况下,您可以让您的类实现该InitializingBean接口(老派)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

至于AbstractRefreshableApplicationContext,有些时候你需要refresh()你的ApplicationContext。发生这种情况的原因可能是您想要重新加载 XML 配置,或者您的环境已更改,但您不想停止/重新启动应用程序。