解决springboot国际化属性文件读取的问题

32 篇文章 0 订阅
31 篇文章 0 订阅

当国际化配置文件的信息无法读取时在国际化时就会出现下面的问题:
在这里插入图片描述
导致的原因:在主配置文件中国际化配置文件的位置指定不正确,错误的指定方式如下,idea之前版本这种指定方式是正确的,可能是由于版本的原因导致的:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190708180141519.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjk0MTQ4Ng==,
解决方法:
改变配置文件中指定国际化配置文件的路径的方式:
在这里插入图片描述
#配置国际化文件位置
spring.messages.basename=i18n/login
在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
您好!针对您的问题,创建前缀一样的文件可以使用Spring框架自带的`@ConfigurationProperties`注解,将同一前缀的属性封装到同一个对象中。具体实现步骤如下: 1. 在`application.properties`配置文件中添加属性: ``` myapp.datasource.url=jdbc:mysql://localhost:3306/mydb myapp.datasource.username=root myapp.datasource.password=123456 ``` 2. 创建一个`MyAppProperties`类,使用`@ConfigurationProperties`注解标记,并指定前缀: ``` @Configuration @ConfigurationProperties(prefix = "myapp.datasource") public class MyAppProperties { private String url; private String username; private String password; // 省略getter和setter方法 } ``` 3. 在Spring Boot的主类中添加`@EnableConfigurationProperties`注解,并将`MyAppProperties`类作为参数传入: ``` @SpringBootApplication @EnableConfigurationProperties(MyAppProperties.class) public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 4. 在需要使用属性的地方注入`MyAppProperties`对象即可: ``` @Service public class MyService { private final MyAppProperties properties; public MyService(MyAppProperties properties) { this.properties = properties; } public void doSomething() { String url = properties.getUrl(); String username = properties.getUsername(); String password = properties.getPassword(); // 使用属性进行业务逻辑处理 } } ``` 这样就可以方便地管理同一前缀的属性了。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值