如何将Spring更改为使用Tomcat vs BasicDataSource中的Datasource? [英] How to change Spring to use Datasource from Tomcat vs BasicDataSource?

查看:405
本文介绍了如何将Spring更改为使用Tomcat vs BasicDataSource中的Datasource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改Spring以使用来自Tomcat vs BasicDataSource的数据源?下面是我在XML中制作的bean的副本.有人可以告诉我如何访问tomcat数据源

How to change Spring to use Datasource from Tomcat vs BasicDataSource? below is a copy of the bean I make in my XML. Can someone tell me how to access the tomcat datasource

<beans:bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" >

        <beans:property
            name="driverClassName"
            value="${database.driver}" />

        <beans:property
            name="url"
            value="${database.url}" />

        <beans:property
            name="username"
            value="${database.user}" />

        <beans:property
            name="password"
            value="${database.password}" />

        <beans:property
            name="initialSize"
            value="5" />

        <beans:property
            name="maxActive"
            value="10" />
    </beans:bean>

推荐答案

在春季,您必须更改配置,使其通过JNDI从tomcat获取配置

In spring you have to change the configuration that it takes the configuration from tomcat via JNDI

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myNewDatasource" />

</beans>

然后,您必须在tomact中配置连接,并使其通过jndi可用. 例如,您可以将其放置在tomcat context.xml中(当然,您需要将驱动程序放置在tomcat的lib目录中)

Then you have to configure the connection within tomact and make it available though jndi. For example you could put this in the tomcat context.xml (and of course you need to put the driver in the tomcat lib directory)

<Resource name="jdbc/myNewDatasource"
            type="javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://SERVER:3306/DB?useUnicode=true&amp;characterEncoding=utf8"

     auth="Container" username="USERNAME" password="PASSWORD"
     maxIdle="3" maxActive="15" maxWait="10000"
     logAbandoned="true" removeAbandoned="true" removeAbandonedTimeout="60"
     validationQuery="select 1" />

这篇关于如何将Spring更改为使用Tomcat vs BasicDataSource中的Datasource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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