旅游后台管理系列——SSM框架Service层整合

整合Service层

所有的service实现类都要放到spring容器中管理。由spring创建数据库连接池,并由spring来管理事务

1)配置applicationContext-service.xml

在src/main/resources/spring目录下新建一个applicationContext-service.xml文件:
旅游后台管理系列——SSM框架Service层整合_第1张图片

<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-4.2.xsd
        http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
   
    
  	<context:component-scan base-package="com.oak.xiyuyou.service"/>
beans>

2)创建service包

在src/main/java下新建service包
旅游后台管理系列——SSM框架Service层整合_第2张图片

2)配置事务

下面我们来配置事务,我们把事务单独提出来进行配置,在src/main/resources新添加applicationContext-tras.xml配置:
旅游后台管理系列——SSM框架Service层整合_第3张图片

<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-4.2.xsd
        http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
   
 	
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource" />
    bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        tx:attributes>
    tx:advice>
    
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.oan.xiyuyou.service.*.*(..))" />
    aop:config>
beans>

3)配置web.xml初始化spring容器


<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>netctoss0702_ssm_demo05display-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

    
	<context-param>
	    <param-name>contextConfigLocationparam-name>
	    <param-value>classpath:spring/applicationContext-*.xmlparam-value>
	context-param>
	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
web-app>

Service整合完毕。

你可能感兴趣的:(旅游后台管理系列——SSM框架Service层整合)