我有一个建立在struts框架上的项目,但是我已经在同一个框架中集成了SpringBoot (使用servlet-config.xml),并在其中使用了MVC和AOP,现在在一个需求中,我必须使用一个构建在SpringBoot上的项目,并使用@ConfigurationProperties将一个application.yml文件绑定到POJO中。
我只需要使用该项目的代码,从而添加该项目的依赖性。是否可以使用@ConfigurationProperties将POJO与项目中的yml文件绑定。我试过了,但是bean没有绑定到yml文件条目。
据我所知,@ConfigurationProperties只是SpringBoot特性的一部分。
请给出你有用的建议。
发布于 2020-02-25 19:47:35
例如,您可以这样做:
如果您的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:https://stackoverflow.com/questions/60397755
复制相似问题