简体   繁体   English

Spring无法使用@PropertySource查找类路径资源

[英]Spring not able to find classpath resource with @PropertySource

I am using @PropertySource in my datasource configuration file to get property files located on classpath. 我在数据源配置文件中使用@PropertySource来获取位于类路径上的属性文件。 Below is my project structure. 下面是我的项目结构。

在此处输入图片说明

I believe I can do it in two ways: 我相信我可以通过两种方式做到这一点:

  1. By creating a package in src folder and add them there. 通过在src文件夹中创建一个软件包并将其添加到那里。 As src folder is already included in the classpath in eclipse, following should work. 由于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”)})

  1. 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. 作为中级Java开发人员,这仍然让我感到困惑。 Can anybody guide me in the right direction. 谁能指导我正确的方向。 And also how we can configure classpath resources for Spring in a production environment. 以及我们如何在生产环境中为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. 编辑 :我已经更改了项目结构,以在src/java/resources包含属性文件,并且可以在构建路径中看到resources文件夹。 Still .properties are not found by spring. 春季仍然找不到.properties

在此处输入图片说明

For anybody facing problem with usign .properties files in Spring 4+, look at the thread below to match your setup with that of OP. 对于任何在Spring 4+中遇到usign .properties文件问题的人,请查看下面的线程以将您的设置与OP匹配。 Setup is all good except for a whitespace in configuration. 除配置中有空格外,安装程序都很好。

Not able to inject .properties file into Spring MVC 4.3 using @PropertySource 无法使用@PropertySource将.properties文件注入Spring MVC 4.3

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

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