软件编程
位置:首页>> 软件编程>> java编程>> 解决@ConfigurationProperties注解的使用及乱码问题

解决@ConfigurationProperties注解的使用及乱码问题

作者:多罗罗~  发布时间:2023-09-08 06:55:10 

标签:@ConfigurationProperties,注解,乱码

@ConfigurationProperties

作用:用于获取配置文件中的属性定义并绑定到javaBean属性中

举个栗子:

配置文件

mycar.name=徐昂
mycar.price=18w

定义实体类

package com.maggie.demo.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data   //生成setget方法
@Component //将此类注册为组件
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件属性读取,读取前缀时mycar的,忽略不存在的字段
public class Car {
   private String name;
   private String  price;
   @Override
   public String toString() {
       return "Car{" +
               "name='" + name + '\'' +
               ", price='" + price + '\'' +
               '}';
   }
}

启动类输出验证

package com.maggie.demo;

import com.maggie.demo.entity.Car;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) throws IOException {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
Car car = run.getBean(Car.class);
System.out.println(car.toString());
}
}

输出结果

Car{name='徐昂', price='18w'}

产生问题,定义中文时,会产生乱码

解决方法

1,将配置文件换成yml文件,则不会产生乱码问题

mycar:
 name: '徐昂'
 price: '18w'

2, 覆盖原文件:org.springframework.boot.env.OriginTrackedPropertiesLoader

将OriginTrackedPropertiesLoader所有代码复制出来,按照包路径建立自己的包和类(包名和类名都必须和原来的一致,不然不生效)

解决@ConfigurationProperties注解的使用及乱码问题

然后找出原来的OriginTrackedPropertiesLoader上的编码片段:

CharacterReader(Resource resource) throws IOException {
           this.reader = new LineNumberReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.ISO_8859_1));
       }

将其改为 : StandardCharsets.UTF_8

CharacterReader(Resource resource) throws IOException {
           this.reader = new LineNumberReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
       }

重启项目,发现项目中文乱码已经解决

Car{name='徐昂', price='18w'}

3, 自定义配置类

解决@ConfigurationProperties注解的使用及乱码问题

配置类

mycar.name=小汽车
mycar.price=18w

定义实体,添加**@PropertySource**注解,指定字符集取utf-8,并指定读取配置文件的路径。

注意这种方法只能对自定义的properties文件有效,对于spring boot默认生成的application.properties没有效果

@Data   //生成setget方法
@Component //将此类注册为组件
//指定字符集,并且指定读取的配置文件
@PropertySource(encoding = "UTF-8", value = "classpath:car.properties", ignoreResourceNotFound = true)
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件属性读取,读取前缀是mycar的
public class Car {
   private String name;
   private String  price;

@Override
   public String toString() {
       return "Car{" +
               "name='" + name + '\'' +
               ", price='" + price + '\'' +
               '}';
   }
}

结果:

Car{name='小汽车', price='18w'}

第二种不推荐写法

@ConfigurationProperties+ @EnableConfigurationProperties

@EnableConfigurationProperties作用:开启组件配置绑定功能,将实体类组件注入到容器中

eg:

mycar.name=小汽车
mycar.price=18w

实体类

package com.maggie.demo.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data   //生成setget方法
@ConfigurationProperties(prefix = "mycar",ignoreUnknownFields = true) //配置文件属性读取,读取前缀时mycar的,忽略不存在的字段
public class Car {
   private String name;
   private String  price;
   @Override
   public String toString() {
       return "Car{" +
               "name='" + name + '\'' +
               ", price='" + price + '\'' +
               '}';
   }
}

配置类

@Configuration //配置类注解 ==配置文件
@EnableConfigurationProperties(Car.class) //开启加载配置类
public class BeansConfiguration {

}

来源:https://blog.csdn.net/qq_51347907/article/details/125678472

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com