标题翻译
How does the spring framework map properties to a Java Object?
问题
我正试图实现与Spring的@ConfigurationProperties
类似的功能,将属性映射到Java POJO,但是在运行时进行。我希望能够重用Spring框架用于处理Spring属性的代码。
我浏览了许多Spring的代码,但是我在隔离执行这一特定功能的代码时遇到了问题。我可以编写自己的算法来实现这一点,但感觉这样做是一种浪费,因为我知道这已经是Spring框架中存在的功能,并且我项目中已经有了Spring的依赖。
英文翻译
I am trying to do something very similar to what spring @ConfigurationProperties
does to map properties to a Java POJO, but at runtime. I am hoping to be able to reuse the spring framework code that does this for spring properties.
I looked through a bunch of spring's code but I am having trouble isolating the code that does this specific functionality. I can write my own algorithm to do this, but it feels like a waste since I know this is existing functionality in the spring framework and I have spring dependencies in my project already.
答案1
得分: 0
经过进一步的挖掘,我找到了以下类:ConfigurationPropertiesBinder
,它将来自不同PropertySources
的属性绑定到一个Bindable
对象上。我能够从这个Bean中提取足够的信息,以创建我自己的版本,以执行我正在寻找的转换。
英文翻译
After some more digging, I found the following class: ConfigurationPropertiesBinder
, which binds properties from various PropertySources
to a Bindable
object. I was able to extract enough information from this bean to create my own version to do the conversion I was looking for.
专注分享java语言的经验与见解,让所有开发者获益!
评论