搜索

5.7. 在 Bean 上使用 init 和 destroy 方法

download PDF

如果您需要在使用 bean 前进行初始化和清理工作,您可以使用 initMethoddestroyMethod (根据 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>

initMethoddestroyMethod 都是可选的,因此 bean 不必同时具有这两个功能。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.