The property file is not found when it's in a jar

你离开我真会死。 提交于 2019-12-25 00:33:48

问题


I've got a problem with my property file since I deploy my application within jars. When it was just in the WEB-INF/classes directory, there was no problem. My problems occur only with properties files inside jars.

Let me explain. I will simplify my code.

My application deals with buisiness objects called IPX (it's not useful to know what it is)

There's a DAO class called IpxDaoImpl with string attributes, that are SQL queries

public class IpxDaoImpl implements IpxDao extends SimpleJdbcDaoSupport {
  private String listAllIpxSql; // query for listing IPX in Database

  public void listAllIpx() {
    // Does a query in Database using the variable listAllIpxSql
    // ...
  }

  public void setListAllIpxSql(String listAllIpxSql) {
    this.listAllIpxSql = listAllIpxSql;
  }
}

This class is defined in a config xml file, called appContext-dao.xml. It contains :

<bean id="ipxDao" class="com.IpxDaoImpl" parent="myGenericDao">

  <property name="listAllIpxSql" value="${ipx.list}" />

</bean>

There is another config xml file, that tells how to load the properties files : appContext-commo-dao.xml. It contains :

<context:property-placeholder location="classpath*:**/sql.properties" />

And there is a sql.properties file proper to queries about IPX. It contains :

ipx.list=SELECT * FROM IPX

Now, when I deploy my application in WEB-INF/classes, everything is fine. But we have an integration environement where we have to generate jars. All the files I've mentioned above are in the same jar. And then, when I deploy, I've got this error : Could not resolve placeholder 'ipx.list' It seems that the property file sql.properties is not found.

I've tried to extract this property file and put it in the WEB-INF/classes directory, with the proper folder. And now it works.

So, what is wrong with my code? Is it the placeholer ? Thanks in advance.

Cedric


回答1:


Try with classpath:/sql.properties

From reference:

Please note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system.

See http://docs.spring.io/spring/docs/2.5.5/reference/resources.html#resources-wildcards-in-path-other-stuff for more info.



来源:https://stackoverflow.com/questions/21044747/the-property-file-is-not-found-when-its-in-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!