天热,外面火辣辣的太阳实在是不扛不住.昨晚吃了泡面折腾了下.今天喉咙就犯病了.疼痛难受.一个人又闲的蛋疼.她昨晚说今天到欢乐谷去玩了.也没人来陪我.一天不写代码手痒.今天就又到公司了.
折腾了下当前最新版本的struts2+spring3+hibernate(jpa)应用的大型网站架构.用到的一些jar包就不折腾了不是很熟悉的可以直接call我了.这里主要是说其三个框架的整合配置文件:
1.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">
<display-name>itcastShop</display-name>
<!--
指定spring的配置文件,默認從web根目錄尋找配置文件,我們可以通過Spring提供的classpath:前綴指定從累路勁下尋找
-->
<context-param>
<!-- 应用路径Spring上下文 配置 -->
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:applicationContext.xml</param-value> -->
<param-value>
/WEB-INF/classes/**/applicationContext*.xml
</param-value>
</context-param>
<!-- Spring的监听 对Spring容器进行实例化 ,并把實例化存放在application的屬性中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--在做上传文件的时候,要在web.xml中增加ActionContextCleanUp这个filter,如果不增加,会发生第一次上传取不到文件的情况-->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<!-- struts2配置文件 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 日志文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--<aop:aspectj-autoproxy/>-->
<context:annotation-config />
<context:component-scan base-package="cn.itcast" />
<context:property-placeholder location="classpath:jdbc.properties" />
<!--
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
-->
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 数据库连接池启动时的初始值 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会去申请一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxWait" value="${jdbc.maxWait}" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="true" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 注入数据源bean到实体管理工厂bean -->
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="travel" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<!-- 注入实体管理工厂bean到事务管理bean -->
<!-- 这是事务定义,而且是使用注解方式定义事务(@Transactional),proxy-target-class="true"表示采用动态代理类来管理事务,
如果是false表示采用接口代理来管理事务(默认值为false)。
-->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 事务声明方式注解 -->
<!-- Activates @Transactional for DefaultImageDatabase -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 上面这个就是使用配置式来定义事务,两种方式的区别主要是,注解式只用写那么一句话,然后在业务类或方法中加入@Transactional这个注解标记,就完成事务声明,
不过对于每个业务类都需要在类或方法中加入这些标记。而配置式声明,就是不用加这些标记,只要你的方法名称命名比较统一,就可以像上面这样定义事务规范,
然后在aop标签中定义切入点与执行通知就行了。我感觉如果业务逻辑不是太复杂的情况,配置式会比较简单,而且修改起来也方便,这两种方式我都写出来了,
至于用哪一种,由你们自己决定。
<aop:config >
<aop:pointcut id="transactionPointcut" expression="execution(* cn.dykj.example.service..*Manager.*(..))"/>
<aop:pointcut id="transactionDao" expression = "execution(* cn.dykj.example.dao..*Dao.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionDao"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
-->
</beans>
3.persistence.xml
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="travel" transaction-type="RESOURCE_LOCAL">
<!--
实体bean集合名字
-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!--
JPA驱动提供者
-->
<class>cn.itcast.bean.user.Buyer</class>
<class>cn.itcast.bean.user.ContactInfo</class>
<!--
<class>cn.itcast.bean.product.ProductInfo</class>
<class>cn.itcast.bean.product.ProductStyle</class>
<class>cn.itcast.bean.product.ProductType</class>
<class>cn.itcast.bean.product.Brand</class>
-->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.jdbc.fetch_size" value="18" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
4.这里连接数据库是jdbc jdbc.properties
jdbc.driverClassName = org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/travel?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.initialSize=1
jdbc.maxActive=500
jdbc.maxIdle=2
jdbc.minIdle=1
jdbc.maxWait=1000
把项目架好配置一些基本的配置就ok了...
分享到:
相关推荐
用SSH+JPA+mysql搭配的基础环境,里面有单元测试,基础页面和sql文件,下载后导入sql和项目,可以直接运行,然后在此基础上直接开发,更方便
医院处方管理系统(SSH框架+JPA+mysql)
WAS6.1用的JDK是IBM jdk1.5, 而且它用的不少jar包比较旧,与最新的SSH2+JPA架构的jar包不兼容,本文是在实践过程中探索到的不兼容问题以及解决方法。基本上是网上查不到的解决方案,谨供需要在生产环境中将最新的SSH2...
spring+springmvc+jpa零配置注解开发 不需要在xml中配置任何东西
关于jsf+ejb+jpa整合,光盘上代码这样写的。
struts2+jpa+spring2.5配置基础框架,直接运行即可
基于SpringBoot+Thymeleaf+JPA的博客系统 基于SpringBoot+Thymeleaf+JPA的博客系统 基于SpringBoot+Thymeleaf+JPA的博客系统 基于SpringBoot+Thymeleaf+JPA的博客系统 基于SpringBoot+Thymeleaf+JPA的博客系统 基于...
基于JSF+EJB3+JPA的竞价拍卖系统,服务器为JBOSS
2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,...
Spring+Struts2+JPA入门小例子
oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA)
该项目是基于SpringBoot + Vue +JPA 的实验室助理信息管理系统-可用于大学生日常课设 功能点描述: (1)学生 学生注册为系统用户、系统用户报名助理岗位、 (2)实验室技术员 筛选报名信息、实验室助理招聘计划的...
基于springboot+jpa+thmleaf的旅游网站系统源码基于springboot+jpa+thmleaf的旅游网站系统源码基于springboot+jpa+thmleaf的旅游网站系统源码基于springboot+jpa+thmleaf的旅游网站系统源码基于springboot+jpa+...
个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa).zip个人博客系统(SpringBoot+jpa)...
springmvc +shiro+querydsl+jpa框架多数据源配置详细说明,文档包含详细配置说明,测试通过
经典JAVA EE企业应用实战基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发——源码.part1 其他部分详见我的上传列表,全部分卷下载完成才能解压。 本书介绍了Java EE规范的三大主要规范JSF、EJB 3和JPA,其中JSF是Sun...
EJB+JSF+JPA全JAVAEE视频教程,EJB+JSF+JPA入门觉得有点难吗?JBOSS报错?该教程全程教你学习搭建EJB+JSF+JPA框架!!!
在学习jpa时候做的一个struts2+spring+jpa整合的完整例子 包含分页,一个简单的 资产入库系统 并实现了登陆等。
本系统是学生信息管理系统,主要演示JPA在实际项目中的使用,采用的是servlet+JSP+JPA.是EJB JPA 这本书里的例子。
用于整合jsf+jpa+spring,绝对详细可靠