JNDI的配置分为两步:服务器容器配置和项目中访问的配置。 关于服务器的配置可参考之前的一篇。 这里只说一下项目中的Spring配置:
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<jpa:repositories base-package="com.spring.jpa.repository"/>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 开启事务管理注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 链接到persistence.xml -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="persistence.xml"/>
<!-- 需要指定数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- JNDI 配置 -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/bookshop"/>
<!-- JNDI的另一种配置 -->
<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">-->
<!--<property name="jndiName" value="java:comp/env/jdbc/bookshop"/>-->
<!--</bean>-->
</beans>
这两种都可以,不过还是推荐使用jee的方式。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/2058.html