05_Spring_SSH三大框架整合

Spring框架的第四天


案例一:SSH框架整合保存客户


需求分析

  • 案例一:SSH框架整合保存客户

技术分析之SSH框架的整合


技术分析之SSH框架开发的基本回顾


技术分析之SSH三大框架需要的jar包

  1. Struts2框架
  • struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar      -- Struts2需要的所有jar包
  • struts2-spring-plugin-2.3.24.jar              ---Struts2整合Spring的插件包
  1. Hibernate框架
  • hibernate-release-5.0.7.Final\lib\required*.jar       -- Hibernate框架需要的jar包
  • slf4j-api-1.6.1.jar                   -- 日志接口
  • slf4j-log4j12-1.7.2.jar                 -- 日志实现
  • mysql-connector-java-5.1.7-bin.jar           -- MySQL的驱动包
  1. Spring框架
  • IOC核心包
  • AOP核心包
  • JDBC模板和事务核心包
  • Spring整合JUnit测试包
  • Spring整合Hibernate核心包
  • Spring整合Struts2核心包

技术分析之SSH三大框架需要的配置文件
1, Struts2框架

  • 在web.xml中配置核心的过滤器
 <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>
  • 在src目录下创建struts.xml,用来配置Action

2, Hibernate框架

  • 在src目录创建hibernate.cfg.xml配置文件
  • 在JavaBean所在的包下映射的配置文件

3, Spring框架

  • 在web.xml配置整合WEB的监听器
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
  • 在src目录下创建applicationContext.xml
  • 在src目录下log4j.proerties

技术分析之Spring框架整合Struts2框架
1, 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action
2, 编写CustomerAction接收请求,在struts.xml中完成Action的配置

 <package name="crm" extends="struts-default" namespace="/">
 <action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}">
 
 </action>
 </package>

3, 在Action中获取到service(开发不会使用,因为麻烦)

  • 可以通过 WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); 来获取,但是这种方式编写代码太麻烦了!!

4, Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)

  • 因为导入的struts2-spring-plugin-2.3.24.jar 包自带一个配置文件 struts-plugin.xml ,该配置文件中有如下代码

  • <constant name="struts.objectFactory" value="spring" /> 开启一个常量,如果该常量开启,那么下面的常量就可以使用

  • struts.objectFactory.spring.autoWire = name,该常量是可以让Action的类来自动装配Bean对象!!


  • struts-spring-plugin-2.3.24.jarstruts.objectFactory后加载,所以会覆盖struts2-core.2.3.24.jar中被注释的struts.objectFactory,那么会struts.objectFactory = spring生效,那么struts.objectFactory.spring.autoWire = name就生效,那么按名称自动注入就生效了(在Action中注入Service对象)。

5, Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

  • 把具体的Action类配置文件applicatonContext.xml的配置文件中,但是注意:struts.xml需要做修改

  • applicationContext.xml

  • <bean id="customerAction" class="com.itheima.web.action.CustomerAction" scope="prototype">

  • struts.xml中的修改,把全路径修改成ID值

  • <action name="customer_*" class="customerAction" method="{1}">

  • 第二种方式需要有两个注意的地方

  • Spring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置scope="prototype"

  • CustomerService现在必须自己手动注入了


技术分析之Spring框架整合Hibernate框架(带有hibernate.cfg.xml的配置文件。强调:不能加绑定当前线程的配置)
1, 编写CustomerDaoImpl的代码,加入配置并且在CustomerServiceImpl中完成注入
2, 编写映射的配置文件,并且在hibernate.cfg.xml的配置文件中引入映射的配置文件

3, 在applicationContext.xml的配置文件,配置加载hibernate.cfg.xml的配置

 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
 <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
 </bean>

4, 在CustomerDaoImpl中想完成数据的添加,Spring框架提供了一个HibernateDaoSupport的工具类,以后DAO都可以继承该类!!

 public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
 public void save(Customer c) {
 System.out.println("持久层...");
 this.getHibernateTemplate().save(c);
 }
 }
 
 <bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl">
 <property name="sessionFactory" ref="sessionFactory"/>
 </bean>

5, 开启事务的配置

  • 先配置事务管理器,注意现在使用的是Hibernate框架,所以需要使用Hibernate框架的事务管理器
 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
  • 开启注解事务
    <tx:annotation-driven transaction-manager="transactionManager"/>

  • 在Service类中添加事务注解
    @Transactional


技术分析之Spring框架整合Hibernate框架(不带有hibernate.cfg.xml的配置文件)
1, Hibernate配置文件中

  • 数据库连接基本参数(4大参数)
  • Hibernate相关的属性
  • 连接池
  • 映射文件

2, 开始进行配置

  • 先配置连接池相关的信息
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver"/>
 <property name="jdbcUrl" value="jdbc:mysql:///xxx"/>
 <property name="user" value="root"/>
 <property name="password" value="root"/>
 </bean>
  • 修改 LocalSessionFactoryBean 的属性配置,因为已经没有了hibernate.cfg.xml的配置文件,所以需要修改该配置,注入连接池
 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
 <property name="dataSource" ref="dataSource"/>
 </bean>
  • 继续在 LocalSessionFactoryBean 中配置,使用hibernateProperties属性继续来配置其他的属性,注意值是properties属性文件
 <!-- 配置其他的属性 -->
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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>com/itheima/domain/Customer.hbm.xml</value>
 </list>
 </property>

技术分析之Hibernate的模板的常用的方法
1, 增删改的操作:

  • 添加:
  • save(Object obj);
  • 修改:
  • update(Object obj);
  • 删除:
  • delete(Object obj);

2, 查询的操作:

  • 查询一条记录:
  • Object get(Class c,Serializable id);
  • Object load(Class c,Serializable id);

3, 查询多条记录:

  • List find(String hql,Object... args);

技术分析之延迟加载问题
1, 使用延迟加载的时候,再WEB层查询对象的时候程序会抛出异常!

  • 原因是延迟加载还没有发生SQL语句,在业务层session对象就已经销毁了,所以查询到的JavaBean对象已经变成了托管态对象!
  • 注意:一定要先删除javassist-3.11.0.GA.jar包(jar包冲突了)
    2,解决方案一:在Customer.hbm.xml中取消延迟加载
    3,解决方案二:Spring框架提供了一个过滤器,让session对象在WEB层就创建,在WEB层销毁。只需要配置该过滤器即可
  • 但是:要注意需要在struts2的核心过滤器之前进行配置
 <filter>
 <filter-name>OpenSessionInViewFilter</filter-name>
 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>OpenSessionInViewFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

修改项目的虚拟目录

  • 项目右键--->Properties---->Web Project Setting



SSH框架整合方式一
0,新建项目,本例命名为day38_ssh1
1,Struts2框架

  • Struts2基础包
    asm-3.3.jar
    asm-commons-3.3.jar
    asm-tree-3.3.jar
    commons-fileupload-1.3.1.jar
    commons-io-2.2.jar
    commons-lang3-3.2.jar
    freemarker-2.3.22.jar
    javassist-3.11.0.GA.jar
    log4j-api-2.2.jar
    log4j-core-2.2.jar
    ognl-3.0.6.jar
    struts2-core-2.3.24.jar
    xwork-core-2.3.24.jar

  • Struts2整合Spring的插件包
    struts2-spring-plugin-2.3.24.jar

2,Hibernate框架

  • Hibernate框架需要的jar包
    antlr-2.7.7.jar
    c3p0-0.9.2.1.jar
    commons-beanutils-1.8.3.jar
    commons-logging-1.1.1.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-c3p0-5.0.7.Final.jar
    hibernate-commons-annotations-5.0.1.Final.jar
    hibernate-core-5.0.7.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.18.1-GA.jar
    jboss-logging-3.3.0.Final.jar
    jstl.jar
    log4j-1.2.16.jar
    mchange-commons-java-0.2.3.4.jar
    standard.jar
  • 日志接口
    slf4j-api-1.6.1.jar
  • 日志实现
    slf4j-log4j12-1.7.2.jar
  • MySQL的驱动包
    mysql-connector-java-5.1.7-bin.jar

3,Spring框架

  • IOC核心包
    com.springsource.org.apache.commons.logging-1.1.1.jar
    com.springsource.org.apache.log4j-1.2.15.jar
    spring-beans-4.2.4.RELEASE.jar
    spring-context-4.2.4.RELEASE.jar
    spring-core-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar

  • AOP核心包
    spring-aspects-4.2.4.RELEASE.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    spring-aop-4.2.4.RELEASE.jar

  • JDBC模板和事务核心包
    spring-jdbc-4.2.4.RELEASE.jar
    spring-tx-4.2.4.RELEASE.jar

  • Spring整合JUnit测试包
    spring-test-4.2.4.RELEASE.jar

  • Spring整合Hibernate核心包
    spring-orm-4.2.4.RELEASE.jar

  • Spring整合Struts2核心包

spring-web-4.2.4.RELEASE.jar

4,Struts2配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
 
</struts>

5,Hibernate配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
 <session-factory>
 <!-- 必须配置 -->
 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day04</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 
 <!-- 可选配置 -->
 <property name="hibernate.show_sql">true</property>
 <property name="hibernate.format_sql">true</property>
 <property name="hibernate.hbm2ddl.auto">update</property>
 
 <!-- 配置C3P0的连接池 -->
 <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
 
 <!-- 映射配置文件 -->
 <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
 </session-factory>
 
</hibernate-configuration> 

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.huachao.domain.Customer" table="cst_customer">
 <id name="cust_id" column="cust_id">
 <generator class="native"/>
 </id>
 
 <property name="cust_name" column="cust_name"/>
 <property name="cust_user_id" column="cust_user_id"/>
 <property name="cust_create_id" column="cust_create_id"/>
 <property name="cust_source" column="cust_source"/>
 <property name="cust_industry" column="cust_industry"/>
 <property name="cust_level" column="cust_level"/>
 <property name="cust_linkman" column="cust_linkman"/>
 <property name="cust_phone" column="cust_phone"/>
 <property name="cust_mobile" column="cust_mobile"/>
 
 </class>
 
</hibernate-mapping>    

6,log4j日志和Spring配置文件
log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout

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.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd">
  
</beans>

7,在web.xml中配置核心过滤器


    <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>

8,在web.xml中配置整合web的监听器


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

<font color=red>注意:</font>

  • 上面所有的配置文件,除了xxx.hbm.xml在domain包目录下,其他的都在src目录下。

9,service层

package com.huachao.service;

import com.huachao.domain.Customer;

public interface CustomerService {
    public void save(Customer c);
    public void update(Customer c);
}

package com.huachao.service;

import com.huachao.domain.Customer;

public class CustomerServiceImp implements CustomerService{
    @Override
    public void save(Customer c) {
        System.out.println("service save ...");
    }
    @Override
    public void update(Customer c) {
        System.out.println("service update ...");
    }
}

10,Action

package com.huachao.web.action;

import com.huachao.domain.Customer;
import com.huachao.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
 private static final long serialVersionUID = 1546062548925314173L;
 
 //记住一定要手动new
 private Customer customer = new Customer();
 @Override
 public Customer getModel() {
 return customer;
 }
 
 // 提供service的成员属性,提供set方法
 private CustomerService customerService;
 public void setCustomerService(CustomerService customerService) {
 this.customerService = customerService;
 }
 
 public String add()
 {
 System.out.println("web123 save ...");
// System.out.println("--------->"+customer.toString());
 customerService.save(customer);
 return NONE;
 }
 public String update()
 {
 System.out.println("web update ...");
 return NONE;
 }
}

11,struts配置和applicationContext配置
struts.xml中添加如下:


    <package name="crm" namespace="/" extends="struts-default">
        <action name="customer_*" class="com.huachao.web.action.CustomerAction" method="{1}"/>
    </package>

applicationContext.xml中添加如下:

<bean id="customerService" class="com.huachao.service.CustomerServiceImp"/>

<font color=red>注意:</font>

  • 在CustomerAction中注入CustomerService,只有导入了struts2-spring-plugin-2.3.24.jar包才可以在Action中通过set方法注入service层的对象

12,Action交给Spring管理
applicationContext.xml


    <bean id="customerService" class="com.huachao.service.CustomerServiceImp"/>
    <!-- 
        Action交给Spring管理
        Spring的bean默认是单例的
        但是Action必须是多例的,因为每一个Action都必须有自己的值栈
        所以加上scope="prototype"声明多例
     -->
    <bean id="customerAction" class="com.huachao.web.action.CustomerAction" scope="prototype">
        <!-- Action交给Spring管理,自动注入失效,需要手动注入service -->
        <property name="customerService" ref="customerService"/>
    </bean>

struts.xml

<package name="crm" namespace="/" extends="struts-default">
        <!-- <action name="customer_*" class="com.huachao.web.action.CustomerAction" method="{1}"/> -->
    
        <!-- 配置客户的Action,如果Action由Spring框架来管理,class标签上只需要编写spring中bean的ID值就OK -->
        <action name="customer_*" class="customerAction" method="{1}"/>
    </package>

13,整合Hibernate
dao接口和实现类

package com.huachao.dao;

import com.huachao.domain.Customer;

public interface CustomerDao {
    public void save(Customer c);
    public void update(Customer c);
}
package com.huachao.dao;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.huachao.domain.Customer;

public class CustomerDaoImp extends HibernateDaoSupport implements CustomerDao {
    @Override
    public void save(Customer c) {
        System.out.println("dao save...");
        getHibernateTemplate().save(c);
    }
    @Override
    public void update(Customer c) {
        System.out.println("dao update...");
    }

}

applicationContext.xml

<!-- 编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
    <!-- 先配置平台事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
    <bean id="customerDao" class="com.huachao.dao.CustomerDaoImp">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

将hibernate配置转移到spring中
1,将day38_ssh1拷贝一份命名为day38_ssh2,
2,在day38_ssh2项目上右键--->Properties---->Web Project Setting,修改为了day38_ssh2
3,在applicationContext.xml中添加

<!-- 先配置C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///spring_04_ssh"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- LocalSessionFactoryBean -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 加载连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载方言,加载可选 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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>com/huachao/domain/Customer.hbm.xml</value>
            </list>
        </property>
        
    </bean>

4,删除hibernate.cfg.xml文件
5,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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 先配置C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///spring_04_ssh"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- LocalSessionFactoryBean -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 加载连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载方言,加载可选 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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>com/huachao/domain/Customer.hbm.xml</value>
            </list>
        </property>
        
    </bean>
    <!-- 先配置平台事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
    <bean id="customerDao" class="com.huachao.dao.CustomerDaoImp">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    
    
    <bean id="customerService" class="com.huachao.service.CustomerServiceImp">
        <property name="customerDao" ref="customerDao"/>
    </bean>
    <!-- 
        Action交给Spring管理
        Spring的bean默认是单例的
        但是Action必须是多例的,因为每一个Action都必须有自己的值栈
        所以加上scope="prototype"声明多例
     -->
    <bean id="customerAction" class="com.huachao.web.action.CustomerAction" scope="prototype">
        <!-- Action交给Spring管理,自动注入失效,需要手动注入service -->
        <property name="customerService" ref="customerService"/>
    </bean>
    
</beans>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容