@ConfigurationProperties
作者:互联网
配置类
@Component public class MyConfig1 { @Bean public User user(){ return new User(); } }
@Data @ConfigurationProperties(prefix = "user") public class User { private String myName; private int age; public User(String myName, int age) { this.myName = myName; this.age = age; } public User(){ } @Override public String toString() { return "User{" + "myName='" + myName + '\'' + ", age=" + age + '}'; } }
application.properties
user.myName=zhangsan
user.age=20
输出类
public static void main(String[] args) { ConfigurableApplicationContext run = SpringApplication.run(StudyWork1Application.class, args); User user = run.getBean(User.class); System.out.println(user); }
输出结果
标签:String,age,myName,ConfigurationProperties,user,public,User 来源: https://www.cnblogs.com/dabo-tian/p/16460154.html