我从团队中获取了源代码,这是一个Spring框架项目。但与以前没什么不同。
好吧,我从Java代码获取属性文件时遇到问题。这是我的简单结构项目:
root
|- src
|- log4j.properties
|- persistent.properties
|- id.com.my.api.elastic.utils
|- StaticVariable.java
我需要正确地将2个属性文件加载到StaticVariable.java
这是我现在的代码:
public static final String PROPERTIES_PATH = "/src/persistent.properties";
public static final String LOG4J_PATH = "/src/log4j.properties";
但是当我在tomcat服务器上运行时仍然找不到此文件,所以我更改了:
public static final String PROPERTIES_PATH = "..\\persistent.properties";
public static final String LOG4J_PATH = "..\\log4j.properties";
也找不到,该如何解决?
这是以前的程序员使用代码的方式:
@Configuration
@ComponentScan(basePackages = "id.com.my.api.elastic")
@PropertySource("file:" + StaticVariable.PROPERTIES_PATH)
public class SpringJDBCConfiguration {
Logger logger = LoggerFactory.getLogger(getClass());
答案 0 :(得分:1)
public static final String PROPERTIES_PATH = "/persistent.properties";
public static final String LOG4J_PATH = "/log4j.properties";
这些路径可以与Class#getResource
和Class#getResourceAsStream
一起使用
关于您的更新:
您正在请求将源文件作为文件-打包应用程序后此文件将不起作用-可能仅在IDE中有效。装有
@PropertySource("file:" + StaticVariable.PROPERTIES_PATH)
应该是
@PropertySource("classpath:/persistent.properties")
和
@PropertySource("classpath:/log4j.properties")