tomcat+JNDI+spring 的配置问题

在做spring有关的项目时,往往需要配置数据源,当然配置的方式有很多种,可以单独写在一个properties文件中,这样修改数据源配置的话比较容易,也比较简单,下面介绍另外一种数据源的配置

利用jndi来配置数据源,该如何配置呢???

一、方式一

1) 添加如下代码到tomcat的conf目录下的server.xml中:

<Context>
<Resource name="jdbc/demoDB" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo"
username="root"
password="123"
maxActive="50"
maxIdle="30"
maxWait="10000" />
</Context>

2)在Spring的配置文件,如applicationContext.xml中配置配置如下内容:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/demoDB</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<!-- 这里是自定义的数据库基础操作类 -->
<bean id="sqlBaseDAO" class="demo.BaseDAOImpl">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
</beans>

二、方式二

在applicationContext.xml(spring配置文件)文件中:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/wydb" />

在web.xml文件中:

<resource-ref >
<description></description>
<res-ref-name>jdbc/wydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

在(tomcat)sever.xml中:

<Context docBase="eWeb" path="/neweb" reloadable="true" source="org.eclipse.jst.j2ee.server:eWeb">
<Resource name="jdbc/wydb" auth="Container" type="javax.sql.DataSource" username="EUSER" password="EUSER" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.130:1521:devdb" maxActive="8" maxIdle="4" factory="org.apache.commons.dbcp.BasicDataSourceFactory" />
</Context>

个人总结

优点:例如在websphere(类似tomcat)加上数据库连接的配置就更安全
缺点:在开发时不易用此配置方法,在用svn将程序下载到本地时任然需要配置,繁琐了些;但是在发布的时候可以用。

转载于:https://www.cnblogs.com/geekdc/p/5190325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值