Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Spring property file configuration for jar file

Java-Spring I have modules based project, i have module for DAO layer and module for business layer which is dependent upon DAO layer and web layer dependent upon DAO layer and business layer.

I am using maven for project compilation. and jar of every components are group under web projects lib folder.

Problem is i have spring context file and .property file inside DAO jar and following is my configuration but i spring unable to load properties i also tried prefixing value="classpath:abc.properties but it didn't work.

When i open the DAO jar both spring context and .properties files are on root.

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="abc.properties" />
  </bean>

<bean id="cmfModelDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="${jdbc.ConnectionUrl}"/>
      <property name="username" value="${jdbc.Username}"/>
      <property name="password" value="${jdbc.Password}"/>
  </bean>

any idea how to quick fix this issue ?

like image 801
d-man Avatar asked Aug 31 '12 20:08

d-man


People also ask

How do I change the properties of a jar file?

Use 7zip, right click on jar and with 7z say open archive, then go to property file, double click and open file with notepad, edit it and save. Now when you will close 7z console, it will ask "update archive?" Say yes... That's it. Save this answer.

How do you specify file path in application properties in spring boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

What is the default configuration file in Spring?

The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.


1 Answers

I have a multi-module web project with Spring using the following code:

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

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${env.datasource.driver}" />
    <property name="url" value="${env.datasource.url}" />
    <property name="username" value="${env.datasource.username}" />
    <property name="password" value="${env.datasource.password}" />
</bean>

Don`t forget to verify the namespace url in the xml file:

xmlns:context="http://www.springframework.org/schema/context";

The folder env must be in classpath, so Spring can find it. My properties file is also inside a jar, and it`s working just fine.

like image 76
gdfbarbosa Avatar answered Oct 04 '22 02:10

gdfbarbosa