Spring,从数据库自动装配@Value
我正在使用属性文件来存储一些配置属性,这些配置属性可以通过以下方式访问:
@Value("#{configuration.path_file}")
private String pathFile;
Is it possible (with Spring 3) to use the same @Value
comment, but要从数据库加载属性而不是一个文件?
I am using a properties File to store some configuration properties, that are accessed this way:
@Value("#{configuration.path_file}")
private String pathFile;
Is it possible (with Spring 3) to use the same @Value
annotation, but loading the properties from a database instead of a file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的数据库中有一个表存储了键/值对:
定义一个新的 bean“applicationProperties” - 伪代码如下...
在应用程序中需要的地方注入此 bean。如果您已经有一个 dao/service 层,那么您只需使用它即可。
Assuming you have a table in your database stored key/value pairs:
Define a new bean "applicationProperties" - psuedo-code follows...
Inject this bean where required in your application. If you already have a dao/service layer then you would just make use of that.
是的,您可以保留 @Value 注释,并在 EnvironmentPostProcessor。
例如,创建一个实现EnvironmentPostProcessor的类:
最后,不要忘记将您的
spring.factories
放入META-INF中。一个例子:Yes, you can keep your @Value annotation, and use the database source with the help of EnvironmentPostProcessor.
For example, create a class which implements EnvironmentPostProcessor:
Finally, don't forget to put your
spring.factories
in META-INF. An example:虽然没有使用过 spring 3,但我假设你可以,如果你创建一个从数据库读取属性并使用 getter 公开它们的 bean。
Although not having used spring 3, I'd assume you can, if you make a bean that reads the properties from the database and exposes them with getters.