I use Spring bean profile to separate each datasource environments:

<bean id="fooJDBCTemplate" class="com.rakuya.r_erp.task.dao.FooJDBCTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

<beans profile="dev">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://somewhere:3306/erp?characterEncoding=UTF-8" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean>
</beans>

<beans profile="qa">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://somewhere:3306/erp?characterEncoding=UTF-8"/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>
</beans>

<beans profile="prod">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://somewhere:3306/erp?characterEncoding=UTF-8"/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>
</beans>

And i use following command to build jar file and specify profile

mvn clean package -Dspring.profiles.active=prod

But i got the error about no definition of dataSource when executing the jar file. It seems like Maven does not use the Spring profile to build jar.

How can I configure the Maven or Spring peofile to use the dataSource ?

Thx

有帮助吗?

解决方案 2

Since your bears are all the same type (DriverManagerDataSource) and the only thing that differs between environments are some of the properties, I think it would be best to get rid of the spring profiles and use properties to set these values. PropertyPlaceholderConfigurer should be able to handle this scenario where you set a vm param specifying what environment you want. Something like -Denv=dev.

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:${env}.properties</value>
        </list>
    </property>
</bean>

Then you would only need one datasource bean like the following:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

And a property file for each environment named dev.properties, qa.properties, and prod.properties with values in each for db.driver, db.url, etc.

When spring starts up it will select the appropriate properties file based on the value passed in with -Denv and load the corresponding property values into your datasource bean.

其他提示

You need to pass the profile -D switch when you run your app.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top