Java 无法在Spring中按属性占位符读取属性文件。获取系统无法找到指定的文件错误
当我试图加载源代码中的属性文件时,我遇到了一些问题。但当我尝试从外部加载属性文件时,它工作正常。下面提到了工作代码和非工作代码。有人能帮我吗。我是春天的一只新蜜蜂:) 我在从外部和项目内部加载属性文件时所做的唯一更改是context:property placeholder中的位置声明 已配置spring上下文以从外部加载属性文件,如下所示。它工作得很好 从外部加载属性文件时-正在工作:Java 无法在Spring中按属性占位符读取属性文件。获取系统无法找到指定的文件错误,java,spring,spring-mvc,spring-batch,property-placeholder,Java,Spring,Spring Mvc,Spring Batch,Property Placeholder,当我试图加载源代码中的属性文件时,我遇到了一些问题。但当我尝试从外部加载属性文件时,它工作正常。下面提到了工作代码和非工作代码。有人能帮我吗。我是春天的一只新蜜蜂:) 我在从外部和项目内部加载属性文件时所做的唯一更改是context:property placeholder中的位置声明 已配置spring上下文以从外部加载属性文件,如下所示。它工作得很好 从外部加载属性文件时-正在工作: <context:property-placeholder location="file:///C:/
<context:property-placeholder location="file:///C:/Test/Data/WebDetails.properties"
ignore-resource-not-found="true" ignore-unresolvable="true" order="1" />
和am映射WebProperties中的属性值,如下所示
@Component
public class WebProperties{
@Value("{url}") public String url;
@Value("{space}") public String space;
//getters
}
我的主要课程是:
public class ProcessWeb {
ApplicationContext context;
WebProperties webProperties;
}
public ProcessWeb () {
context = new ClassPathXmlApplicationContext("web-context.xml");
webProperties= (WebProperties) context.getBean("webProperties");
}
public void execute(String[] args){
webProperties.getURL()
}
public static void main(String args[])
{
ProcessWeb main = new ProcessWeb ();
main.execute();
}
当我从代码中加载属性时执行我的主类。我收到的错误为${url}(系统找不到指定的文件)。
如何在占位符中配置位置路径
更多信息:将从批处理文件调用主类ProcessWeb。在部署代码之后,当我们从命令提示符执行批处理文件时,对于外部属性也会遇到同样的问题。是否需要更改任何配置?我们正在将代码打包到jar文件中使用
类路径
如下:
location="classpath:WebDetails.properties"
我以前试过这个。它在从代码执行时工作。但从命令行看,它不起作用。更多信息:将从批处理文件调用主类ProcessWeb。在部署代码之后,当我们从命令提示符执行批处理文件时,我遇到了同样的问题。是否需要更改任何配置?我们正在将代码打包到jar文件中。@User0234主类和ProcessWeb都需要WebDetails.properties。或者至少哪个罐子需要它。检查jar文件的内容。可能jar中不存在属性文件。
public class ProcessWeb {
ApplicationContext context;
WebProperties webProperties;
}
public ProcessWeb () {
context = new ClassPathXmlApplicationContext("web-context.xml");
webProperties= (WebProperties) context.getBean("webProperties");
}
public void execute(String[] args){
webProperties.getURL()
}
public static void main(String args[])
{
ProcessWeb main = new ProcessWeb ();
main.execute();
}
location="classpath:WebDetails.properties"