用于本地主机,开发和生产的Spring数据源配置 [英] Spring datasource configuration for localhost, development, and production

查看:231
本文介绍了用于本地主机,开发和生产的Spring数据源配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的Spring应用程序可以确定它的部署位置,并加载适当的数据源。我们有3个环境...我的本地,开发服务器和生产服务器。到目前为止,我有3个属性文件名为

I'm trying to figure out how my Spring app can determine where it is deployed and load the appropriate datasource. We have 3 environments...my local, the development server, and the production server. So far I have 3 properties files called

localhost.datasource.properties
development.datasource.properties
production.datasource.properties

我有他们这样配置:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/resources/properties/production.datasource.properties</value>
                <value>classpath:/resources/properties/development.datasource.properties</value>
                <value>classpath:/resources/properties/localhost.datasource.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSourceMySQL" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${mysql.jdbc.driver.class.name}"
        p:url="${mysql.jdbc.url}"
        p:username="${mysql.jdbc.username}"
        p:password="${mysql.jdbc.password}" />

</beans>

当我在本地机器上时,这个工作正常。如果我部署一个war文件到开发它仍然读取localhost属性,因为它是在列表中的最后一个,我得到一个错误。

This works fine when I am on my localhost machine. If I deploy a war file to development it is still reading the localhost properties since it is last in the list and I get an error. What is the best way to implement this?

感谢

推荐答案

对于数据源,最简单的方法是定义数据源,并让容器管理连接池。

For data sources the easiest way would be to define data sources and let the container manage the connection pooling.

为此,定义对web中的数据源的资源引用.xml

To do so, define a resource reference to the data source in web.xml

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/MyDataSource</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

并在春天引用它:

<jee:jndi-lookup 
     id="dataSource" 
     jndi-name="jdbc/MyDataSource" />

,那么您可以在应用程序服务器中定义数据源,这意味着您可以更改基础数据库。在websphere的情况下,这将通过websphere控制台。在tomcat的情况下,它将通过Context.xml:

then you can define the data source in the application server, which means you can change the underlying database. In case of websphere, this would be done through the websphere console. In case of tomcat, it would be done through the Context.xml:

<Context>
    ...
  <Resource name="jdbc/MyDataSource" auth="Container" type="javax.sql.DataSource"
            maxActive="100" maxIdle="30" maxWait="10000"
            username="javauser" password="javadude"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/javatest"/>
</Context>

这样,您只需要更改上下文以部署到开发,测试和生产,将您的应用程序绑定到特定数据库。

That way you only need to change the context to deploy to development, test and production and don't tie your application to a specific database.

这篇关于用于本地主机,开发和生产的Spring数据源配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆