eclipse Spring 无法使用 @PropertySource 找到类路径资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40232870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring not able to find classpath resource with @PropertySource
提问by Rama Arjun
I am using @PropertySource
in my datasource configuration file to get property files located on classpath. Below is my project structure.
我@PropertySource
在我的数据源配置文件中使用来获取位于类路径上的属性文件。下面是我的项目结构。
I believe I can do it in two ways:
我相信我可以通过两种方式做到这一点:
- By creating a package in
src
folder and add them there. Assrc
folder is already included in the classpath in eclipse, following should work.
- 通过在
src
文件夹中创建一个包并将它们添加到那里。由于src
文件夹已包含在 eclipse 的类路径中,因此应该可以执行以下操作。
@PropertySources({ @PropertySource("classpath: com/spring/property/general.properties"), @PropertySource("classpath: com/spring/property/hibernate.properties") })
@PropertySources({ @PropertySource("classpath: com/spring/property/general.properties"), @PropertySource("classpath: com/spring/property/hibernate.properties") })
- Second way is to create a
resources
folder and add it to the classpath and following should work
- 第二种方法是创建一个
resources
文件夹并将其添加到类路径中,以下应该可以工作
@PropertySources({ @PropertySource("classpath: general.properties"), @PropertySource("classpath: hibernate.properties") })
@PropertySources({ @PropertySource("classpath: general.properties"), @PropertySource("classpath: hibernate.properties") })
In my case neither of the two is working. Being an intermediate java developer this still confuses me. Can anybody guide me in the right direction. And also how we can configure classpath resources for Spring in a production environment.
在我的情况下,两者都不起作用。作为一名中级 Java 开发人员,这仍然让我感到困惑。任何人都可以指导我朝着正确的方向前进。以及我们如何在生产环境中为 Spring 配置类路径资源。
EDIT:
I have changed my project structure to include properties file in src/java/resources
and I can see the resources
folder in build path. Still .properties
are not found by spring.
编辑:我已经更改了我的项目结构以在其中包含属性文件,src/java/resources
并且我可以resources
在构建路径中看到该文件夹。仍然.properties
没有找到春天。
回答by Rama Arjun
For anybody facing problem with usign .properties
files in Spring 4+, look at the thread below to match your setup with that of OP. Setup is all good except for a whitespace in configuration.
对于.properties
在 Spring 4+ 中遇到使用文件问题的任何人,请查看下面的线程以将您的设置与 OP 的设置相匹配。除了配置中的空格外,设置都很好。
Not able to inject .properties file into Spring MVC 4.3 using @PropertySource