0

I'm in the process of transitioning the legacy Spring 3.2.8 application from WAR deployment to self-contained JAR. I'm using tomcat7-maven-plugin to create an executable JAR:

mvn install tomcat7:exec-war

Since the application uses JNDI placeholders, it obviously fails to run without them defined. In Tomcat installation following placeholders are defined: (conf/Catalina/localhost/myapp.xml):

<Resource name="jdbc/mediaagent" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/mediaagent" username="user" password="password"/>
<Environment name="hibernate/dialect" type="java.lang.String" value="PostgreSQL" />

The context.xml which is packaged in WAR, has following placeholder references:

<ResourceLink name="jdbc/mediaagent" global="jdbc/mediaagent" type="javax.sql.DataSource"/>
<ResourceLink name="hibernate/dialect" global="hibernate/dialect" type="java.lang.String"/>

Finally, these placeholders are used in applicationContext.xml in the following way:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/mediaagent"/>
</bean>



    <bean id="dataSourceProperties"
              class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="properties">
                <props>
                ...
                    <prop key="hibernate.dialect">org.hibernate.dialect.${dialect}Dialect</prop>
                ...
                </props>
            </property>
        </bean>

My goal is to extract these settings to separate configuration file which should be placed next to the self-contained JAR, preferably properties file as in Spring Boot. There also should be the possibility to inject them with JNDI as with WAR file. What is the most elegant way achieve it?

yuiu
  • 93
  • 10
  • _like_ boot or _with_ boot ? https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Marged Dec 15 '15 at 19:15
  • @Marged: *like* Spring Boot. We use Spring 3.2.8 so I cannot use Spring Boot. – yuiu Dec 15 '15 at 19:38
  • 1
    Why not simply upgrade Spring, generally it is a drop in replacement, which would give you Spring Boot , which in turn saves you reinventing the wheel. I would suggest not to use JNDI but simply property files and let Spring configure the datasource. The `${dialect}` can also be set as a system variable instead of JNDI `-Ddialect=Oracle`. The datasource replace with a spring configured instead of JNDI datasource. – M. Deinum Dec 16 '15 at 08:31

0 Answers0