Spring 处置工厂模式?
我可以使用工厂 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的数据库 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.