天热,外面火辣辣的太阳实在是不扛不住.昨晚吃了泡面折腾了下.今天喉咙就犯病了.疼痛难受.一个人又闲的蛋疼.她昨晚说今天到欢乐谷去玩了.也没人来陪我.一天不写代码手痒.今天就又到公司了.
折腾了下当前最新版本的struts2+spring3+hibernate(jpa)应用的大型网站架构.用到的一些jar包就不折腾了不是很熟悉的可以直接call我了.这里主要是说其三个框架的整合配置文件:
1.web.xml
2.applicationContext.xml
3.persistence.xml
4.这里连接数据库是jdbc jdbc.properties
把项目架好配置一些基本的配置就ok了...
折腾了下当前最新版本的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了...