在Spring中动态创建bean [英] Dynamic creation of beans in Spring

查看:121
本文介绍了在Spring中动态创建bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

春天有一种方法,我们可以从数据库表中读取一个bean的字段,并创建一个完整的bean类 - 在服务器启动时使用getter和setter?



我需要这样做使我的应用程序完全可配置...如果我将来添加一个新的字段,我将需要在db中添加一个字段,而bean setter和getter将是



谢谢

解决方案

动态注册bean。您可以使用 BeanDefinitionBuilder 为此目的请参阅此处的示例。但是,如@Darren所说,通过数据库查找来撬动一个bean不是一个明智的主意。


Is there a way in spring wherein we can read the fields of a bean from the DB table and create a complete bean class - with getters and setters on server startup????

I require this to make my application completely configurable...as in if I have to add a new field in future , all I would require would be adding a field in the db and the bean setters and getters would be available to me.

Thanks

解决方案

You could try approaches for dynamically registering beans . You could use the BeanDefinitionBuilder for this purpose . See a sample here . But as @Darren says , It's not a wise idea to creak a bean via DB lookup .

这篇关于在Spring中动态创建bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆