您需要从属性类中删除@Component并添加setter,因为标准的bean属性绑定被使用
@ConfigurationProperties:
@ConfigurationProperties(prefix="my")public class ConfigBinder { private String name; private String url; // expected to be filled automatically public String getUrl() { return this.url; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; }}
并将@EnableConfigurationProperties添加到您的主类中:
@SpringBootApplication@EnableConfigurationProperties(ConfigBinder.class)public class Application { public static void main(String[] args) { final ApplicationContext ctx = SpringApplication.run(Application.class, args); final ConfigBinder confs = ctx.getBean(ConfigBinder.class); System.out.println(confs.getUrl()); System.out.println(confs.getName()); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)