- TestConfig
@Configuration
@ConfigurationProperties(prefix = "com.springboot")
@PropertySource("classpath:test.properties")
public class TestConfig {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- test.properties
com.springboot.age = 10
- controller
@RestController
@EnableConfigurationProperties(Student.class)
public class HelloController {
@Autowired
private Student student;
@RequestMapping("hello")
public String hello() {
return testConfig.getAge()+"";
}
}
结果
访问http://localhost:8080/hello