JBoss 配置JNDI

1:在JBOSS中配置数据源

在D:\jboss-4.2.3.GA\docs\examples\jca中有各种数据库的数据源模板

先mysql模板修改为如下数据,拷贝到D:\jboss-4.2.3.GA\server\default\deploy中:


<?xml version="1.0" encoding="UTF-8"?>

<datasources>
  <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/sharp?characterEncoding=UTF-8</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>密码</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

2:在web项目中引用数据源

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <resource-ref>
        <res-ref-name>MySqlDS1</res-ref-name>
        <jndi-name>MySqlDS</jndi-name>
    </resource-ref>
</jboss-web>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<resource-ref>
		<res-ref-name>MySqlDS1</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
	</resource-ref>
  
</web-app>

3:spring中引用JNDI

	<!-- jboss 配置-->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:MySqlDS</value>
		</property>
	</bean>  


你可能感兴趣的:(JBoss 配置JNDI)