简体   繁体   English

如何在 jar 中加载类路径 @PropertySource?

[英]How to load classpath @PropertySource inside a jar?

  • I am using spring-context 5.3.13 (no spring-boot)我正在使用 spring-context 5.3.13(没有 spring-boot)
  • I have a file named "config.yaml" under "src/main/resources"我在“src/main/resources”下有一个名为“config.yaml”的文件
  • I have this @PropertySource configured: @PropertySource(value = "classpath:config.yaml", factory = TypesafeAdapterPropertySourceFactory.class)我配置了这个@PropertySource: @PropertySource(value = "classpath:config.yaml", factory = TypesafeAdapterPropertySourceFactory.class)
  • When I run it from intellij it successfully loads the file当我从 intellij 运行它时,它成功加载了文件

My issue: When running a jar the file is not found due to the path being unavailable inside the jar.我的问题:运行 jar 时,由于 jar 中的路径不可用,找不到该文件。 More info:更多信息:

  • the file itself is inside the jar root (checked it)文件本身在 jar 根目录内(检查过)
  • Exception:例外:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.viber.httpmsexample.Application];线程“主”org.springframework.beans.factory.BeanDefinitionStoreException 中的异常:无法解析配置 class [com.viber.httpmsexample.Application]; nested exception is java.io.FileNotFoundException: class path resource [config.yaml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Repositories/http-ms-example/target/app.jar./config.yaml at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser:java.189) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor:java.331) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor:java.247) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBe nested exception is java.io.FileNotFoundException: class path resource [config.yaml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Repositories/http-ms-example/ target/app.jar./config.yaml at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser:java.189) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor:java.331) at org .springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor:java.247) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBe anDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate:java.311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate:java.112) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext:java.746) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext:java.564) at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext:java.93) at com.mytest.httpmsexample.Application.main(Application:java:63) Caused by. anDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate:java.311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate:java.112) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext:java.746) at org.springframework .context.support.AbstractApplicationContext.refresh(AbstractApplicationContext:java.564) at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext:java.93) at com.mytest.httpmsexample.Application.main(Application:java:63)造成的。 java.io:FileNotFoundException. java.io:FileNotFoundException。 class path resource [config:yaml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C./Repositories/http-ms-example/target/app.jar./config.yaml at org.springframework.util.ResourceUtils:getFile(ResourceUtils.java.217) at org.springframework.core.io.AbstractFileResolvingResource:getFile(AbstractFileResolvingResource.java.162) at com.mytest.libraries.anticorruption.config.TypesafeAdapterPropertySourceFactory:createPropertySource(TypesafeAdapterPropertySourceFactory.java.14) at org.springframework.context.annotation.ConfigurationClassParser:processPropertySource(ConfigurationClassParser.java.463) at org.springframework.context.annotation.Confi class 路径资源 [config:yaml] 无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/C./Repositories/http-ms-example/target/app.Z68995FCBF434492D150.D48/ yaml at org.springframework.util.ResourceUtils:getFile(ResourceUtils.java.217) at org.springframework.core.io.AbstractFileResolvingResource:getFile(AbstractFileResolvingResource.java.162) at com.mytest.libraries.anticorruption.config.TypesafeAdapterPropertySourceFactory: createPropertySource(TypesafeAdapterPropertySourceFactory.java.14) at org.springframework.context.annotation.ConfigurationClassParser:processPropertySource(ConfigurationClassParser.java.463) at org.conf.conf gurationClassParser:doProcessConfigurationClass(ConfigurationClassParser.java.280) at org.springframework.context.annotation.ConfigurationClassParser:processConfigurationClass(ConfigurationClassParser.java.250) at org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser.java.199) at org.springframework.context.annotation.ConfigurationClassParser:doProcessConfigurationClass(ConfigurationClassParser.java.304) at org.springframework.context.annotation.ConfigurationClassParser:processConfigurationClass(ConfigurationClassParser.java.250) at org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser.java.207) at org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser.java.17 gurationClassParser:doProcessConfigurationClass(ConfigurationClassParser.java.280) at org.springframework.context.annotation.ConfigurationClassParser:processConfigurationClass(ConfigurationClassParser.java.250) at org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser.java.199) at org .springframework.context.annotation.ConfigurationClassParser:doProcessConfigurationClass(ConfigurationClassParser.java.304) at org.springframework.context.annotation.ConfigurationClassParser:processConfigurationClass(ConfigurationClassParser.java.250) at org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser .java.207) 在 org.springframework.context.annotation.ConfigurationClassParser:parse(ConfigurationClassParser.java.17 5)... 8 more 5)... 8 更多

I can externalize this config but I wish to keep it there for now.我可以将此配置外部化,但我希望暂时保留它。 Is it possible to get over that issue?有可能克服这个问题吗?

Thanks!谢谢!

The problem is your own TypesafeAdapterPropertySourceFactory which tries to create the property source based on a File which doesn't work when the file is in a jar.问题是您自己的TypesafeAdapterPropertySourceFactory尝试基于File创建属性源,当文件位于 jar 中时该文件不起作用。 A File in the sense of java is a physical file on the filesystem, one that is inside an archive isn't. java 意义上的文件是文件系统上的物理文件,存档内的文件不是。

I would strongly suggest to use the ResourcePropertySource to create the property source and decorate that instead of something else.我强烈建议使用ResourcePropertySource来创建属性源并装饰它而不是其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM