您的位置:首页 > 运维架构 > Tomcat

spring mvc项目,部署tomcat 中,使用jndi数据源

2017-10-26 10:58 489 查看
最近部署项目时,有几个项目使用同一个数据源,不想在每个项目中都配置一边,所以就想用jndi数据源的方式,tomcat是8.5,spring是最新版本,配置如下:

conf/context.xml全局配置:

<Context>

<!-- Default set of monitored resources. If one of these changes, the    -->
<!-- web application will be reloaded.                                   -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->

<Resource name="test" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="15" maxWaitMillis="10000"
username="test" password="test" driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost"/>
</Context>


web.xml增加配置:

<resource-ref>
<description>Datasource</description>
<res-ref-name>test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


spring applicationcontext.xml配置:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>test</value>
</property>
<property name="resourceRef">
<value>true</value>
</property>
</bean>


resourceRef必须配置,否则报错

Name [test] is not bound in this Context. Unable to find [test]


另外官网说要这么写:java:/comp/env/名字,改成下面这样:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:/comp/env/test</value>
</property>
<property name="resourceRef">
<value>true</value>
</property>
</bean>


我也测试了,可以用,网上搜到很多,基本都不能用,不知道是不是版本的问题。

官网配置文档,在这里

支持的属性,可以在这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat spring jndi