第十四讲:14,spring整合

1,新建一个web动态项目,2.5 ;添加struts2的jar包(17个包),hibernate包(14个包),spring4的包(13个包),一个数据库驱动包(1个包);web.xml文件;src下,spring文件(applicationContext.xml),struts文件(struts.xml)和hibernate配置文件(hibernate.cfg.xml)。
以上需要的jar包和配置文件下载地址:
链接:https://pan.baidu.com/s/1zOiYuekK7CB5r85NI3pl8g 密码:c675
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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
  
    
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName"
           value="com.mysql.jdbc.Driver">
       property>
       <property name="url"
           value="jdbc:mysql://localhost:3306/test">
       property>
       <property name="username" value="root">property>
       <property name="password" value="root">property>
    bean>
      
      
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        property>  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  
          
        <property name="packagesToScan">  
            <list>  
                <value>com.cruise.entityvalue>  
            list>  
        property>  
    bean>  
  
      
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    bean>  
  
      
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
          
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        tx:attributes>  
    tx:advice>  
      
   
      
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.cruise.service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    aop:config>  
  
      
    <context:component-scan base-package="com.cruise" />  
  
beans>
2,建包,com.cruise.dao 层,com.cruise.dao.impl层,com.cruise.service层 com.cruise.service.impl层 com.cruise.action层,com.cruise.entity层。
14spring整合
3,在WebContent目录下新建一个index.jsp做测试。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
你好Ashley
body>
html>

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83747632
今日推荐