7.7. 在 Bean 上使用 init 和 销毁 方法
如果您需要在使用 bean 之前进行初始化和清理工作,您可以使用 initMethod
和 destroyMethod
(由 Camel 触发)。
这些方法必须是 public void
且没有参数,如下所示:
初始化和清理方法
public class MyBean { public void initMe() { // do init work here } public void destroyMe() { // do cleanup work here } }
public class MyBean {
public void initMe() {
// do init work here
}
public void destroyMe() {
// do cleanup work here
}
}
您还必须在 XML DSL 中声明这些方法,如下所示:
初始化和清理 XML
<bean name="myBean" type="com.acme.MyBean" initMethod="initMe" destroyMethod="destroyMe"> <constructors> <constructor index="0" value="true"/> <constructor index="1" value="Hello World"/> </constructors> </bean>
<bean name="myBean" type="com.acme.MyBean"
initMethod="initMe" destroyMethod="destroyMe">
<constructors>
<constructor index="0" value="true"/>
<constructor index="1" value="Hello World"/>
</constructors>
</bean>
initMethod
和 destroyMethod
都是可选的,因此 bean 不必同时具有。