我有一个基于 struts 框架构建的项目,但我已将 Spring Framework 集成到同一项目中(使用 servlet-config.xml
)并使用 其中包含 MVC 和 AOP,现在在一项要求中,我必须使用一个基于 SpringBoot 构建并使用 @ConfigurationProperties
的项目将 application.yml
文件与 POJO 绑定(bind)。
我只需要使用该项目的代码,因此添加了该项目的依赖项。有什么方法可以使用 @ConfigurationProperties 来将 POJO 与项目中的 yml 文件绑定(bind)。我尝试过,但 bean 未与 yml 文件条目绑定(bind)。
据我所知,@ConfigurationProperties 仅是 SpringBoot 功能的一部分。
请提出您有用的建议。
最佳答案
您可以执行如下所示的操作:
如果您的 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:
关于java - @ConfigurationProperties在spring中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397755/