这是我的问题:我试图在context.xml tom cat的字段中配置数据源,如下所示:

//C:\apache-tomcat-7.0.42\conf\context.xml

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>


<Resource
  name="jdbc/myDB" docBase="inv" auth="Container"
  type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
  maxActive="100" maxIdle="30" maxWait="10000"
  url="jdbc:mysql://localhost:3306/myDB?autoReconnect=true"
  username="root" password="pass"/>

</Context>


在我的servlet-context.xml(春季项目)中,我配置了hibernate:

...
<!-- JDBC Data Source -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/rhcimax"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>

<!-- Hibernate Session Factory -->
<bean id="mySessionFactory"     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
        <value>com.blah.baseProject</value>
    </array>
</property>
<property name="hibernateProperties">
  <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </value>
</property>
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>


我该怎么做才能删除代码的以下部分并将其委托给tom cat的context.xml?

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/rhcimax"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>


提前致谢。

最终解决方案:

参考是:xmlns:jee =“ http://www.springframework.org/schema/jee”,xsi:schemaLocation =“ http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd

最后,我们有:

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

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

<!-- Hibernate Session Factory -->
<bean id="mySessionFactory"     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
        <value>com.blah.baseProject</value>
    </array>
</property>
<property name="hibernateProperties">
  <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </value>
</property>
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>


谢谢alanstroop。

更多信息:http://static.springsource.org/spring/docs/3.3.0.BUILD-SNAPSHOT/spring-framework-reference/html/xsd-config.html

最佳答案

09-15 23:00