学习淘淘商城第九课(SSM框架整合之Service层整合)

  上节课我们一起学习了Dao层的整合,这节课我们一起来学习下Service层整合。

          我们需要在src/main/resources/spring目录下新建一个applicationContext-service.xml文件,如下图所示。


       applicationContext-service.xml文件的内容如下所示,可以看到我们配置包扫描器,扫描所有带@Service注解的类。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
  7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd  
  8.     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-4.2.xsd  
  9.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">  
  10.       
  11.     <!-- 配置包扫描器,扫描所有带@Service注解的类 -->  
  12.     <context:component-scan base-package="com.taotao.service"/>  
  13.       
  14. </beans>  
        service一般是由接口和实现类组成,因此我们需要先新建接口所在的目录,我们把它放在taotao-manager-interface工程下,接口的实现类放在taotao-manager-service工程下,我们在taotao-manager-interface工程的src/main/java目录下新建com.taotao.service包,在taotao-manager-service工程的src/main/java目录下新建com.taotao.service.impl包,如下图所示。


      下面我们来配置事务,我们把事务单独提出来进行配置,我们新建一个applicationContext-trans.xml文件,如下图所示。


      applicationContext-trans.xml文件的内容如下所示。其中事务的传播行为需要说明一下,当接口名以save、insert、add、create、delete、upate开头时spring会自动帮我们开启事务(前提是我们配置了事务传播行为),而find、select、get开头的接口是查询,不涉及更改数据库,因此不需要事务,spring不会为查询接口自动开启事务。下面再说说切面,也就是事务的作用范围,execution(* com.taotao.service.*.*(..))的意思是,com.taotao.service下的任意类的任意方法的任意参数及任意返回值都是事务的切入点。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
  7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd  
  8.     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-4.2.xsd  
  9.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">  
  10.       
  11.     <!-- 事务管理器 -->  
  12.     <bean id="transactionManager"  
  13.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  14.         <!-- 数据源 -->  
  15.         <property name="dataSource" ref="dataSource" />  
  16.     </bean>  
  17.     <!-- 通知 -->  
  18.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  19.         <tx:attributes>  
  20.             <!-- 传播行为 -->  
  21.             <tx:method name="save*" propagation="REQUIRED" />  
  22.             <tx:method name="insert*" propagation="REQUIRED" />  
  23.             <tx:method name="add*" propagation="REQUIRED" />  
  24.             <tx:method name="create*" propagation="REQUIRED" />  
  25.             <tx:method name="delete*" propagation="REQUIRED" />  
  26.             <tx:method name="update*" propagation="REQUIRED" />  
  27.             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
  28.             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />  
  29.             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />  
  30.         </tx:attributes>  
  31.     </tx:advice>  
  32.     <!-- 切面 -->  
  33.     <aop:config>  
  34.         <aop:advisor advice-ref="txAdvice"  
  35.             pointcut="execution(* com.taotao.service.*.*(..))" />  
  36.     </aop:config>  
  37.       
  38. </beans>  

       我们在服务层新建了三个配置文件,那么程序是怎么知道这三个文件的呢?这就需要在服务层初始化spring容器,方法是在taotao-manager-service工程下的web.xml文件中进行配置。web.xml文件所在的位置如下图所示。


       web.xml文件的内容如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.  id="WebApp_ID"  
  4.  xmlns="http://java.sun.com/xml/ns/javaee"  
  5.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  7.  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  8.  <display-name>taotao-manager</display-name>  
  9.  <welcome-file-list>  
  10.     <welcome-file>index.jsp</welcome-file>  
  11.  </welcome-file-list>  
  12.    
  13.     <!-- 初始化spring容器 -->  
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>classpath:spring/applicationContext-*.xml</param-value>  
  17.     </context-param>  
  18.     <listener>  
  19.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  20.     </listener>  
  21.    
  22. </web-app>  

      这样,我们的service层便配置完了。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值