spring的@Value注解获取配置属性,但需要每个单独注入,这种方式已过时,
@Value("${com.kfit.blog.name}")
现在可以使用@ConfigurationProperties注解进行编码,引用配置类
<!--spring boot 配置处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
注解使用示例:
自定义配置项需要统一前缀
/**
* prefix="com.kfit.blog" :
*
* 在application.properties配置的属性前缀,
* 在类中的属性就不用使用{@value}进行注入了。
*/
@ConfigurationProperties(prefix="com.kfit.blog")
public class BlogProperties {
private String name;//博客作者
private String title;//博客标题
// 省略getter和setter
}
在启动类中加入:@EnableConfigurationProperties({BlogProperties.class})