gpt4 book ai didi

java - Spring 上下文和 Bean 生命周期回调 : practical examples of usage

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:34 25 4
gpt4 key购买 nike

我对Spring有点经验。我想知道 Spring 上下文/Bean 生命周期中的回调量。我从未使用过它们,并且可以想象其中大部分需要的情况。

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

上下文回调: enter image description here

Bean 回调: enter image description here

附言:

我很清楚大多数回调何时调用,或者 ApplicationContext 的一个或另一个实现是为了什么而编写的。但我不明白为什么有人可能想从回调\实现中获利。例如:

  • AbstractRefreshableApplicationContext 用于动态更改 bean 配置。但为什么?在哪种情况下我可能想即时更改 bean 的配置?
  • afterPropertiesSet 回调,显然是在设置完所有 bean 的属性后调用 :) 但为什么我应该知道它,以及我应该(可能想要)使用它的时间?

最佳答案

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

查看每个接口(interface)的 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>

像这样的类

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 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 接口(interface)(老派)

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注解(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 接口(interface)通常与 FactoryBean 接口(interface)一起使用。它有助于在生产对象之前初始化工厂。有关更多示例,请参阅这两个接口(interface)的 javadoc 并查找各种实现类的源代码。对其他 *Aware 接口(interface)执行相同的操作。

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

关于java - Spring 上下文和 Bean 生命周期回调 : practical examples of usage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21163355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com