I have a project with spring and maven. I found that using profiles, maven could change the properties of the data source. But what if in production the datasource is with a lookup like this, how to do the profiles for this: one with a basic datasource and the other a jee lookup.
<jee:jndi-lookup id="dataSourcejndi" jndi-name="jdbc/BGGDS"
default-value="null" resource-ref="true"/>
This is how the profiles are in the pom.xml
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<hibernate.dialect>org.hibernate.dialect.HSQLDialect</hibernate.dialect>
<jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
<jdbc.url>jdbc:hsqldb:hsql://localhost/test</jdbc.url>
<jdbc.username>sa</jdbc.username>
<app.datasource>dataSource</app.datasource>
<jdbc.password />
<jdbc.isolation />
</properties>
</profile>
<profile>
<id>hudson</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<hibernate.dialect>org.hibernate.dialect.HSQLDialect</hibernate.dialect>
<jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
<jdbc.url>jdbc:hsqldb:hsql://localhost/othertest</jdbc.url>
<app.datasource>dataSource</app.datasource>
<jdbc.username>sa</jdbc.username>
<jdbc.password />
</properties>
</profile>
</profiles>
and this the configutarion of spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xml开发者_如何学JAVAns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!--Datasource pruebas" -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!-- La definición del Factory de Session con Anotaciones -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="${app.datasource}" />
<property name="hibernateProperties">
<props>
<!--Pruebas
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>-->
<!--Produccion-->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Test.hbm.xml</value>
</list>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- Injected properties
-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
And this work for normal datasources, but what if I need a lockup.
IMO, you should go for two Spring configuration files, for example data-access.xml and data-access-test.xml, and use the later in a testing context. You can of course mix this approach with profiles and filtering, pickup one or the other depending on the profiles, etc. There are actually infinite possibilities.
I had a situation a few months back where I had a Spring enabled JNDI connection for running on the app server and a Spring enabled c3p0 pooled connection for a desktop debugger of the same app.
In order to accomplish this, I had a two separate spring configs for the db. One for JNDI, and one for the local pooled version. I used a separate "parent" config which included the appropriate configuration based upon a filtered property in the parent config.
Long story short, use the profile to set the configuration file that you will include into your parent spring file.
A simple, but very powerful option is to package different files with your application, i.e. different Spring configuration files.
精彩评论