5.7. 在 Bean 上使用 init 和 destroy 方法
如果您需要在使用 bean 前进行初始化和清理工作,您可以使用 initMethod
和 destroyMethod
(根据 Camel 触发)。
这些方法必须是 公共 void
,且没有参数,如下所示:
初始化和清理方法
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>
initMethod
和 destroyMethod
都是可选的,因此 bean 不必同时具有这两个功能。