日期:2014-05-16  浏览次数:20371 次

JSF2.1+Spring3.1+JPA(Hibernate3.6)配置实例

????? 首先搭建环境,就不具体说了,我用的是eclipse(不是myeclipse),导入必要的jar包,需要特别注意,Spring的两个AOP的jar包也要倒进去,一开始我认为Aop跟我都应用无关,所以没有导入这两个包,带来不少麻烦。

????? 这里主要讲配置。有两种情况:Pure JPA + Spring和Spring JpaDaoSupport。这两种情况大同小异,主要是细节上有些差别。

????? 一、Pure JPA + Spring 四个配置文件分别如下:

???? 1、applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testcases"/>
<property name="username" value="root"/>
<property name="password" value="beijing123"/>
</bean>

<!-- 这里不需要配置 entityFactory -->

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
-->
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="testcasemanager"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" />
-->

<bean id="atcDao" class="com.semc.conversation.DAO.AutoTestCaseDaoImpl">
</bean>

<bean id="caseManager" class="com.semc.conversation.service.CaseManager">
<property name="atcDao" ref="atcDao"/>
</bean>

</beans>

??? 2、face-config.xml

<faces-config
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-facesconfig_2_0.xsd"
version="2.0">

<application>

<variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
<!--
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
-->
</application>


<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.LoginBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>coBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.CaseOperatorBean</managed-bean-clas