无法在Spring中通过property-placeholder读取属性文件。获取系统找不到指定文件的错误

时间:2022-05-18 22:05:34

When I tried to load property file which is in the source code, I am getting issues. But when I tried to load property file externally, it working fine. Mentioned both working and not working code below. Can someone help me on this. I am a very new bee to Spring :)

当我尝试加载源代码中的属性文件时,我遇到了问题。但是当我尝试从外部加载属性文件时,它工作正常。下面提到了工作代码和非工作代码。有人可以帮我这个。春天我是一个非常新的蜜蜂:)

The only change I did for loading property file from external and within the project is location declaration in context:property-placeholder.

我从外部和项目中加载属性文件所做的唯一更改是上下文中的位置声明:property-placeholder。

Configured spring context to load property file from external as below. It worked very well.

配置弹出上下文以从外部加载属性文件,如下所示。它运作得很好。

When loaded property file from externally - working :

从外部加载属性文件时 - 工作:

<context:property-placeholder location="file:///C:/Test/Data/WebDetails.properties"
    ignore-resource-not-found="true" ignore-unresolvable="true" order="1" />

I configured my property file to load from project in a spring context as below - Not Working:

我将我的属性文件配置为在spring上下文中从项目加载,如下所示:Not Working:

<bean id="webProperties" class="com.test.run.WebProperties" />

<context:property-placeholder location="file:./src/main/resources/WebDetails.properties"
    ignore-resource-not-found="true" ignore-unresolvable="true" order="1" />

My WebDetails.properties is place in Maven Project under src/main/resources. And file looks like

我的WebDetails.properties位于src / main / resources下的Maven Project中。和文件看起来像

url = www.testresponse.com
space = SampleSpace

And am mapping property values in WebProperties like below

并在WebProperties中映射属性值,如下所示

@Component
public class WebProperties{
   @Value("{url}") public String url;
   @Value("{space}") public String space;

   //getters
}

My Main class is :

我的主要课程是:

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();
}

When I executed my main class when load properties from code. I am getting error as ${url} (The system cannot find the file specified.)

当我从代码加载属性时执行我的主类。我收到错误$ {url}(系统找不到指定的文件。)

How Should I configure My location path in placeholder?

如何在占位符中配置我的位置路径?

More Info : Main class, ProcessWeb , will be called from a batch file. After deploying the code, when we executing that batch file from command prompt, am getting same issue for external properties as well. Is any configuration need to be change? We are packaging our code into jar files

更多信息:将从批处理文件中调用主类ProcessWeb。部署代码后,当我们从命令提示符执行该批处理文件时,外部属性也会遇到同样的问题。是否需要更改任何配置?我们将代码打包到jar文件中

1 个解决方案

#1


2  

Use classpath like:

使用类路径,如:

location="classpath:WebDetails.properties"

#1


2  

Use classpath like:

使用类路径,如:

location="classpath:WebDetails.properties"