Springboot未阅读外部属性文件

fcg9iug3  于 2022-12-13  发布在  Spring
关注(0)|答案(1)|浏览(135)

尝试通过外部属性文件读取SpringBoot应用程序中的属性。

@SpringBootApplication
@Configuration
@PropertySource({"file:${DOCUMENT_CONVERTER_PROPERTIES}"})
public class ConverterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConverterApplication.class, args);
    }
}

此处,DOCUMENT_CONVERTER_PROPERTIES是环境变量中添加的文件F:\PROPPERTIES\converter\documentConverter. properties的保持器。
ConverterService.java

@Service
public class ConverterService {

    @Autowired
    private Environment environment;

    String tempPDFPath = environment.getProperty(Constants.TEMP_PDF_PATH);
    String tempDocxPath = environment.getProperty(Constants.TEMP_DOCX_PATH);

ConverterServiceTest.java

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SpringExtension.class)
//@TestPropertySource("classpath:/")
public class ConverterServiceTest {

    @Autowired
    private ConverterService service;
    private byte[] bytes = null;

    @Test
    void testConvertDocxToPDF(){
        createFile("<path>/myFile.docx"); //put this file at given location
        service.convertDocxToPDF(bytes);
    }

这里缺少什么,任何帮助都是感激的提前感谢!!

wlzqhblo

wlzqhblo1#

在Windows中生成URI时出现问题。您的PropertySource导致
file:F:\PROPPERTIES\converter\documentConverter.properties .
我认为正确的格式是
file:///F:/PROPPERTIES/converter/documentConverter.properties
路径中的反斜杠可能有效,但开头的斜杠(file:///)是强制性的...

相关问题