版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ErickPang/article/details/53692040
最近在使用jboss结合spring配置数据源,遇到过一些问题也从网上找了很多资料,基本上大同小异全部都是如何去配置,但是也没有解决问题,终于解决了在此记录下如何配置以及待解决的问题,供参考。
1、使用环境
jdk1.7+jboss7.1.1 final+spring4.0+oracle数据库
2、配置jboss数据源
1)进入jboss-as-7.1.1.Final\modules\com目录新建 oracle\ojdbc6\main(使用的是ojdbc6.jar)
2、新建module.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.oracle.ojdbc6">
<resources>
<resource-root path="ojdbc6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
其中name实际的main上级目录对应,resource-root path实际的Jar包名称
3)将ojdbc6.jar复制到main目录下。
4)配置standalone.xml文件,
文件位置:jboss-as-7.1.1.Final\standalone\configuration
找到datasources节点调整如下配置
<!--配置jndi名称必须为:java:/或者java:jboss/+自定义数据源名称-->
<datasource jndi-name="java:jboss/datasources/ddccDataSource" pool-name="ddccDataSource" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:@ip:1521:数据库实例</connection-url>
<driver>oracle</driver>
<pool>
<min-pool-size>3</min-pool-size>
<max-pool-size>10</max-pool-size>
<prefill>true</prefill>
<use-strict-min>false</use-strict-min>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<!--支持明文和密文密码的方式-->
<security>
<!--明文方式-->
<user-name>dbname</user-name>
<password>dbpwd</password>
<!--密文方式-->
<!--
<security-domain>OPS</security-domain>
-->
</security>
</datasource>
<!--module指定的也需要跟module.xml中name保持一致-->
<drivers>
<driver name="oracle" module="com.oracle.ojdbc6">
<!--增加oracle驱动-->
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
如果密码使用了密文即使用了security-domain节点,则找到security-domains节点,增加配置:
<!--name需跟上边配置保持一致-->
<security-domain name="OPS">
<!--注意authentication跟原有文件中的不一样authorization-->
<authentication>
<login-module code="SecureIdentity" flag="required">
<!--用户名-->
<module-option name="username" value="用户名"/>
<!--加密后的密文-->
<module-option name="password" value="密文"/>
</login-module>
</authentication>
</security-domain>
3、配置spring applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
<!--需要添加否则没有标签提示,启动也会报找不到jee-->
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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
<!--需要添加否则没有标签提示,启动也会报找不到jee-->
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
">
<!--使用jee方式 id自定义,jndi-name跟jboss配置保持一致-->
<jee:jndi-lookup id="dataSource" jndi-name="数据源名称" resource-ref="true" />
遇到的问题:
1、配置spring使用以下方式
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/ddccDataSource"/>
<property name="resourceRef" value="true"></property>
</bean>
就会报找不到jndi:javax.naming.NameNotFoundException: 不知道什么原因,也希望有知道的大神能够留言指导。