springboot如何自定义properties文件
在springboot中,如果我们想加载外部的配置文件,但是又不想与其他的配置文件产生明显的耦合,那么我们可以把这些配置文件,单独弄成一个独立的配置文件,比如下面的配置文件,我们想把这些配置移动到user.properties中:
user2: id: 2 user-name: zhangsan
移动到user.properties变成如下:
user2.id=3 user2.user-name=lisi
编写user2的配置类,如下:
@PropertySource(value = {"classpath:user.properties"})
@ConfigurationProperties(prefix = "user2")
@Component
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class UserPropertiesConfiguration {
private Long id;
private String userName;
}这里的关键主要是@PropertySource和@ConfigurationProperties(prefix = “user2”)和@Component
使用方式如下:
@Resource
private UserPropertiesConfiguration userPropertiesConfiguration;
@GetMapping("/debug4")
public R debug4() {
return R.successs(userPropertiesConfiguration);
}这样子就可以完成了,在迁移的过程中@PropertySource不支持yml语法,所以要变成properties,想要变成yml文件的小伙伴要注意哦。
到此这篇关于springboot如何自定义properties文件的文章就介绍到这了,更多相关springboot自定义properties文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring中Bean有关NullPointerException异常的原因分析
在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerException,解决方法包括避免在静态方法中使用注入的bean,或者使用Spring的ApplicationContext来获取bean,但后者不推荐2024-12-12
tomcat启动完成执行 某个方法 定时任务(Spring)操作
这篇文章主要介绍了tomcat启动完成执行 某个方法 定时任务(Spring)操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-09-09


最新评论