config/author.properties
author.name=jack
author.age=24
AuthorConfig.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:/config/author.properties")
@Data
public class AuthorConfig {
private String name;
private Integer age;
}
@RestController
public class AuthorController {
@Autowired
AuthorConfig authorConfig;
@RequestMapping("author")
public String getAuthor(){
return authorConfig.toString();
}
}
参考:
https://www.jianshu.com/p/b71845c142d0
spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案