这是我的问题:我试图在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(spring项目)中有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。
答案 0 :(得分:3)
&lt; jee:jndi-lookup id =“myDataSource”jndi-name =“java:comp / env / jdbc / myDB”/&gt;
答案 1 :(得分:0)
您还可以使用jee:jndi-lookup + Spring配置文件。这个example向您展示了如何使用这两个Spring功能。 Spring Profiles允许您在不对应用程序进行任何修改的情况下使用不同的环境。