How to specify relative path for hibernate.javax.cache.uri property in xml based spring configuration

2.5k Views Asked by At

I have legacy spring application which is built using xml based configuration I am trying to configure session factory with second level cache.

I have ecache.xml file present in resource folder also hibernate.javax.cache.uri property expects absolute path for ecache.xml.

if i provide URI as file:///c:/App/resources/ecache.xml it works. But this is not good for deployments, maintenance.

how can specify relative path like classpath:ehcache.xml or /WEB-INF/ehcahe.xml in spring xml based configurations ?

Note: I am not using spring boot.

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.example.vl.model</value>
            </list>
        </property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.javax.cache.uri">file:///c:/App/resources/ecache.xml</prop>
            </props>
        </property>
    </bean>
2

There are 2 best solutions below

0
On

OK, I am able to get absolute uri in xml based configuration using Spring EL As follows.

<prop key="hibernate.javax.cache.uri">#{ new org.springframework.core.io.ClassPathResource("/ehcache.xml").getURI().toString()}</prop>
0
On