springboot读取配置文件的方式以及乱码解决

it2024-03-29  14

springboot读取配置文件的方式有三种 1、使用@value注解的方式进行读取 2、使用Environment读取 3、使用@ConfigurationProperties注解读取 4、读取配置遇到的乱码

1、使用@Value注解的方式进行读取

@Value注解方式一

application.properties user.name=gzh name=gzh username=gzh userName=gzh student.name=gzh user.age=30 读取类 @RestController @RequestMapping("/user") public class UserController { @Value("${user.name}") private String username$; @Value("${name}") private String name; @Value("${username}") private String username; @Value("${userName}") private String userName; @Value("${user.age}") private String age; @Value("${student.name}") private String studentName; @RequestMapping("/readConfig") public Object readConfig(){ Map map = new HashMap(); map.put("name",name); map.put("age",age); map.put("studentName",studentName); map.put("userName",userName); map.put("username",username); map.put("username$",username$); return map; } } 浏览器访问(http://localhost:8080/user/readConfig) ==请注意:==在application.properties中的key user.name=gzh name=gzh username=gzh userName=gzh student.name=gzh user.age=30

user.name、username、userName读出来的数据都是Administrator,系统默认的去查了电脑的用户名,所有最好不要这么用

@Value注解方式二

applicattion.properties demo.name=zhangjh demo.age=30 实体类‘’ @Component @Data public class Demo { @Value("${demo.name}") private String demoName; @Value("${demo.age}") private String demoAge; } 读取配置文件的类 @RestController @RequestMapping("/demo") public class DemoController { @Autowired private Demo demo; @RequestMapping("/readConfig") public Object readConfig(){ Map map = new HashMap(); map.put("demoName",demo.getDemoName()); map.put("demoAge",demo.getDemoAge()); return map; } } 访问地址 2、使用Environment读取 applicattion.properties demo.name=zhangjh demo.age=30 读取配置文件的类 @RestController @RequestMapping("/demo2") public class Demo2Controller { @Autowired private Environment environment; @RequestMapping("/readConfig") public Object readConfig(){ Map map = new HashMap(); map.put("demoName",environment.getProperty("demo.name")); map.put("demoAge",environment.getProperty("demo.age")); return map; } } 访问地址 3、使用@ConfigurationProperties注解读取 在src\main\resources下新建config.properties配置文件:,config.properties demo.name=zhangjh demo.age=30 demo.sex=男 java实体类 @Component @ConfigurationProperties(prefix = "demo") @PropertySource(value = "config.properties") @Data public class User { private String name; private Integer age; private String sex; } @Component 表示将该类标识为Bean @ConfigurationProperties(prefix = "demo")用于绑定属性,其中prefix表示所绑定的属性的前缀。 @PropertySource(value = "config.properties")表示配置文件路径。 使用配置文件中的配置 @RestController @RequestMapping("/demo3") public class Demo3Controller { @Autowired private User user; @RequestMapping("/readConfig") public Object readConfig(){ Map map = new HashMap(); map.put("name",user.getName()); map.put("age",user.getAge()); map.put("sex",user.getSex()); return map; } } 访问地址 注意:出现了乱码 解决方案 4、读取配置遇到的乱码

选择File–>setting–>file encoding进行设置 最后,复制config.properties文件中的内容并保存到一个临时文本中,把config.properties文件删除,在原来的地方重新新建一个config.properties,然后把拷贝出去的内容复制到新的config.propertis 文件中,然后重新访问,乱码解决

最新回复(0)