SpringBoot之配置文件注入

application.properties内容:

neo.title=纯洁的微笑
neo.description=分享技术,品味生活
redis.port = 3306
redis.ip = 127.0.0.1

普通注入值

@Value("${neo.title}")
private String title;

自定义配置文件

redis.properties

redis.ip=127.0.0.1
redis.port=3306

创建实体类

@Getter @Setter
@Component
@ConfigurationProperties(prefix="redis")
@PropertySource("classpath:redis.properties")
public class RedisProperties {
    private String ip;
    private String port;
}

@Resource
private RedisProperties properties;

如上,@PropertySource注解指定properties文件
@ConfigurationProperties指定文件中参数的前缀
@Resource为properties作注入

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容