微服务加载不到配置文件的值

239 阅读1分钟

问题情况如下:

这是一个微服务项目,其中common中的文件如下:

由于common是公共的处理模块,所以使用META-INF/spring.factories进行了Spring启动时的注入

Tess4jClient

@Data
@Component
@ConfigurationProperties(prefix = "tess4j")
public class Tess4jClient {

    private String dataPath;
    private String language;

    public String doOCR(BufferedImage image) throws TesseractException {
        //创建Tesseract对象
        ITesseract tesseract = new Tesseract();
        //设置字体库路径
        tesseract.setDatapath(dataPath);
        //中文识别
        tesseract.setLanguage(language);
        //执行ocr识别
        String result = tesseract.doOCR(image);
        //替换回车和tal键  使结果为一行
        result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
        return result;
    }
}

可以看到我们是想要在这个Tess4jClient类中进行配置文件的相同值的读取操作

再看下spring.factories文件的内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.heima.common.exception.ExceptionCatch,\
  com.heima.common.swagger.SwaggerConfiguration,\
  com.heima.common.swagger.Swagger2Configuration,\
  com.heima.common.aliyun.GreenImageScan,\
  com.heima.common.aliyun.GreenTextScan,\
  com.heima.common.tess4j.Tess4jClient

下面是service业务层的项目模块结构图

当我们启动WemediaApplication时就会注入Tess4jClient类的,但是读取配置文件内容是从service模块中读取的,所以我们需要将需要读取的内容写到service模块下的bootstrap.yml文件中,如下内容:

server:
  port: 51803
spring:
  application:
    name: leadnews-wemedia
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yml

tess4j:
  data-path: E:\tessdata
  language: chi_sim

注意:如果写到了common模块中那么就会读取不到值的