来自:http://blog.csdn.net/zndxlxm/article/details/8714383
在三大框架集成中需要几个配置文件,包括如下:
web.xml配置文件:
(1)配置struts2的过滤器 (2)spring框架的配置文件 (3)会话监听器 (4)编码的配置
- <span style="font-size:14px;"><?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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <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>/*</url-pattern>
- </filter-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>CharacterEncodingFilter</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>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app></span>
(1)读取dbcp数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
(2)Hibernate的常用属性配置(注意到一定要配置方言dialect,否者会报错的)
(3)事务管理的配置
(4)bean的配置
- <span style="font-size:14px;"><?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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:property-placeholder location="classpath:dbcpconfig.properties" />
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>${driverClassName}</value>
- </property>
- <property name="url">
- <value>${url}</value>
- </property>
- <property name="username">
- <value>${username}</value>
- </property>
- <property name="password">
- <value>${password}</value>
- </property>
- <property name="initialSize">
- <value>${initialSize}</value>
- </property>
- <property name="maxActive">
- <value>${maxActive}</value>
- </property>
- <property name="maxIdle">
- <value>${maxIdle}</value>
- </property>
- <property name="minIdle">
- <value>${minIdle}</value>
- </property>
- <property name="maxWait">
- <value>${maxWait}</value>
- </property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>cn/csu/edu/graduateDesign/domain/TestUser.hbm.xml</value>
- </list>
- </property>
- </bean>
- <!-- hibernate的事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 事务传播特性 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="modify*" propagation="REQUIRED" />
- <tx:method name="*" propagation="NOT_SUPPORTED" />
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut expression="execution (* cn.csu.edu.graduateDesign.service.*.*(..))"
- id="AllMethod" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="AllMethod" />
- </aop:config>
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">
- <property name="testUserService" ref="testUserService"/>
- </bean>
- <bean id="testUserService" class="cn.csu.edu.graduateDesign.service.impl.TestUserServiceImpl">
- <property name="testUserDao" ref="testUserDao"/>
- </bean>
- <bean id="testUserDao" class="cn.csu.edu.graduateDesign.dao.impl.TestUserDaoHibernateImpl">
- <property name="hibernateTemplate" ref="hibernateTemplate"/>
- </bean>
- </beans></span><span style="font-size: 18px;">
- </span>
定义的一些类,在这里面利用set注入的方式进行实例化了,这里面的实例互相引用,我相信大家应该看得
懂)
struts.xml配置文件:
(1)在这个配置文件里面有这样一句配置的情况
<constant name="struts.objectFactory" value="spring"/>
这句话的意思是Struts2的action由Spring来负责进行实例化
<!-- 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring -->
(2)这个配置文件的主要作用就是进行action的管理,一般先设定包
<package name="user" extends="struts-default">
<action name="user" class="TestUserAction">
<result>/user_list.jsp</result>
<result name="login" type="redirect">/index.jsp</result>
</action>
</package>
/*注意这里面的class没有写完全的路径,因为这里面的这个class的值只需要
和application.xml的id要相同,就是如下这个配置
- <bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">
- <property name="testUserService" ref="testUserService"/>
- </bean>
*/
dbcp的properties文件:进行数据源的配置(我是连接的oracle数据库)
- #连接设置
- driverClassName=oracle.jdbc.driver.OracleDriver
- url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
- username=liuxiaoming
- password=liuxiaoming
- #<!-- 初始化连接 -->
- initialSize=10
- #最大连接数量
- maxActive=50
- #<!-- 最大空闲连接 -->
- maxIdle=20
- #<!-- 最小空闲连接 -->
- minIdle=5
- #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
- maxWait=60000
- #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
- #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
- connectionProperties=useUnicode\=true;characterEncoding\=utf-8
- #指定由连接池所创建的连接的自动提交(auto-commit)状态。
- defaultAutoCommit=true
- #driver default 指定由连接池所创建的连接的只读(read-only)状态。
- #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
- defaultReadOnly=
- #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
- #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
- defaultTransactionIsolation=READ_UNCOMMITTED
我们发现在集成之后,3大框架的配置文件互相关联,hibernate的配置文件被集成到了application.xml里面了
spring里面封装了hibernate模板,struts里面action的实例化也交给了spring,在整个数据操作的时候的事务也
被进行AOP设置,这些都给我们开发带来了很大的方便