Spring 处置工厂模式?

发布于 2025-01-02 18:55:58 字数 508 浏览 5 评论 0原文

我可以使用工厂 bean 在 Spring 中创建另一个 bean。是否也可以通过将一个豆子交给另一个豆子来处理(销毁)它?

例如,我有 Database bean,它充当 Corpus bean 的工厂:

<bean id="Corpus" name="Corpus" factory-bean="Database"
    factory-method="loadCorpus">
</bean>

loadCorpus 上,数据库从服务器读取数据并将其作为语料库对象。

现在我想在语料库中进行一些更改,并在程序生命周期结束时将其放回数据库。

在“处置”语料库 bean 的过程中将其返回给数据库进行处置是合乎逻辑的。

但我没有看到这方面的配置选项。虽然可以使用 bean 和方法来设置创建,但只能使用方法来进行销毁。

那怎样才能达到我的目标呢?

I can use factory bean to create another beans in Spring. Is it also possible to dispose (destroy) of a bean by giving it to another one?

Fore example I have Database bean which serves as a factory for Corpus beans:

<bean id="Corpus" name="Corpus" factory-bean="Database"
    factory-method="loadCorpus">
</bean>

Upon loadCorpus, the database reads data from server and returns it as a corpus object.

Now I want to make some changes inside the corpus and put it back to database at the end of the program lifetime.

It would be logical to make this during the process of 'disposing' of corpus bean by giving it back to database for disposal.

But I see no configuration options for this. While creation can be set both with a bean and a method, the destroyment is only possible with a method.

How to reach my goal then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2025-01-09 18:55:58

让您的数据库 bean 保留对其创建的 Corpus 实例的引用,并使其 一次性。这样,当容器停止时,将调用 Database bean 的 destroy 方法,并且它可以保留对其创建的 Corpus 实例所做的所有更改。

Make your Database bean keep a reference to the Corpus instance it creates, and make it disposable. This way, when the container stops, the destroy method of the Database bean is called, and it can persist all the changes made to the Corpus instances it has created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文