- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对Spring有点经验。我想知道 Spring 上下文/Bean 生命周期中的回调量。我从未使用过它们,并且可以想象其中大部分需要的情况。
我的问题是:您能为每个回调提供至少一个使用示例吗? 表示您需要该回调的情况。
上下文回调:
Bean 回调:
附言:
我很清楚大多数回调何时调用,或者 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/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!