本文介绍了我可以在没有Spring AppContext的情况下手动加载@ConfigurationProperties吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在不直接使用Spring Context的情况下加载标有@ConfigurationProperties的类?基本上,我想重用Spring所做的所有智能逻辑,但是对于在bean生命周期之外手动实例化的bean.

Is there any way to load a class marked with @ConfigurationProperties without using a Spring Context directly? Basically I want to reuse all the smart logic that Spring does but for a bean I manually instantiate outside of the Spring lifecycle.

我有一个可以在Spring(引导)中愉快地加载的bean,可以将其注入到我的其他Service bean中:

I have a bean that loads happily in Spring (Boot) and I can inject this into my other Service beans:

@ConfigurationProperties(prefix="my")
public class MySettings {
    String property1;
    File property2;
} 

有关更多信息,请参见spring docco http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-command-line-args

See the spring docco for more info http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-command-line-args

但是现在我需要从Spring之外(由Hibernate创建)的类访问此bean.该类是在应用程序启动过程的早期创建的,以至于Spring Boot尚未通过经典的查找帮助器方法或我自己拥有"的静态引用来使应用程序上下文可用.

But now I need to access this bean from a class that is created outside of Spring (by Hibernate). The class is created so early in the app startup process that Spring Boot has not yet made the application context available through the classic lookup helper methods or roll-my-own static references.

所以我反而想做类似的事情:

So I instead want to do something like:

MySettings mySettings = new MySettings(); 
SpringPropertyLoadingMagicClass loader = new SpringPropertyLoadingMagicClass();
loader.populatePropertyValues(mySettings);

并让MySettings最终从命令行,系统属性,app.properties等中加载所有其值.Spring中是否有某个类可以执行此类操作,或者是否与应用程序上下文交织在一起?

And have MySettings end up with all its values loaded, from the command line, system properties, app.properties, etc. Is there some class in Spring that does something like this or is it all too interwoven with the application context?

很显然,我可以自己加载Properties文件,但是我真的想保持Spring Boot的逻辑围绕使用命令行变量(例如--my.property1 = xxx),系统变量,application.properties甚至是yaml.文件,以及围绕轻松绑定和类型转换的逻辑(例如,property2是一个File),因此它们的工作方式与在Spring上下文中使用时完全相同.

Obviously I could just load the Properties file myself, but I really want to keep Spring Boot's logic around using command line variables (e.g. --my.property1=xxx), or system variables, or application.properties or even a yaml file, as well as its logic around relaxed binding and type conversion (e.g. property2 is a File) so that it all works exactly the same as when used in the Spring context.

可能是白日梦吗?

感谢您的帮助!

推荐答案

您要查找的魔术"类是PropertiesConfigurationFactory.但是我会问您是否需要它-如果只需要绑定一次,那么Spring应该可以为您完成它,如果您遇到生命周期问题,最好解决这些问题(以防它们破坏其他东西).

The "magic" class you are looking for is PropertiesConfigurationFactory. But I would question your need for it - if you only need to bind once, then Spring should be able to do it for you, and if you have lifecycle issues it would be better to address those (in case they break something else).

这篇关于我可以在没有Spring AppContext的情况下手动加载@ConfigurationProperties吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 11:12