ssh(struts,spring,hibernate)开发的初步集成02--xml文件配置

一.web.xml的配置

  1.修改<web-app>的dtd约束,改为当前tomcat安装目录-->conf文件夹-->web.xml中的dtd配置..复制过去即可

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

  2.配置spring加载配置文件的监听器

  //该配置是给listener指定要加载的xml文件地址(spring的xml),如果不配置.

  则系统会默认会去/WEB-INF/下加载applicationContext.xml。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

  //该配置是spring的核心监听器

<listener>
  <listener class>org.springframework.web.context.ContextLoaderListener</listener-      class>

</listener>

  3.配置spring的OpenSessionInView过滤器(使得在页面上能够保持session的打开)

<!-- openSessionInView:在视图上打开session -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

  4.配置struts2的核心拦截器

  

<filter>
<filter-name>struts2filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

   5.在根目录resource文件夹下面创建applicationContext-common.xml(名字自取)

      a.打开使用注解

      <context:annotation-config></context:annotation-config>

      b.设置为自动扫描包,包全路径名称之间,用逗号隔开!     

      <context:component-scan        base-  package="com.lemon.action,com.lemon.service,com.lemon.dao,        com.lemon.p  o"></context:component-scan>

      c.配置一个数据源

        

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/sshtest  characterEncoding=UTF-8">   
</property>
<property name="username" value="root"></property>
<property name="password" value="abc123"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>

    d.生成SessionFactory

<!-- 生成SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 使用某个数据源生成SessionFactory -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.lemon.po.User</value>
<value>com.lemon.po.Department</value>
</list>
</property>
</bean>

    e.配置 事务管理器(使用sessionfactory)

    

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

    f.配置事务管理的通知(使用事务管理器,指明了在哪些命名的方法上如何使用事务)

  

<!-- 配置事务管理的通知 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置事务的传播特性 -->
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<!-- 本操作内如果有事务,则使用事务,如果没有,则开启新的事务 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

    f.切面配置(在业务层上进行事务管理)

<aop:config>
<aop:pointcut expression="execution(* com.lemon.service.impl.*.*(..))" id="transactionPointcut"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>

 

posted @ 2017-05-19 10:58  霍乱时期的爱情  阅读(154)  评论(0编辑  收藏  举报