我有一个基于struts框架构建的项目,但是我已经将 Spring框架集成在同一框架中(使用servlet-config.xml
)并使用 MVC 和< em> AOP ,现在我必须使用一个在 SpringBoot 上构建的项目,并使用@ConfigurationProperties
将application.yml
文件与POJO绑定在一起
我只需要使用该项目的代码,即可添加该项目的依赖关系。有什么方法可以使用@ConfigurationProperties
将POJO与项目中的yml文件绑定。我试过了,但是bean没有与yml文件条目绑定。
据我所知@ConfigurationProperties
仅是 SpringBoot 功能的一部分。
请提出您有用的建议。
答案 0 :(得分:0)
您可以执行以下示例:
如果您的yml文件具有包含相同前缀的属性,则可以按以下方式使用:
POJO类:
@Configuration // add this if your spring boot version is < 2.2 or else please donot add.
@ConfigurationProperties(prefix = "spring.db.props") //example
public class MyPojoProps{
private String userName;
private int password;
private String uri;
// standard getters and setters
}
您的application.yml可能类似于以下内容:
application.yml
spring:
db:
props:
userName:
password:
uri: