上节课我们一起学习了Dao层的整合,这节课我们一起来学习下Service层整合。
我们需要在src/main/resources/spring目录下新建一个applicationContext-service.xml文件,如下图所示。
applicationContext-service.xml文件的内容如下所示,可以看到我们配置包扫描器,扫描所有带@Service注解的类。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.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-4.2.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
- <!-- 配置包扫描器,扫描所有带@Service注解的类 -->
- <context:component-scan base-package="com.taotao.service"/>
- </beans>
下面我们来配置事务,我们把事务单独提出来进行配置,我们新建一个applicationContext-trans.xml文件,如下图所示。
applicationContext-trans.xml文件的内容如下所示。其中事务的传播行为需要说明一下,当接口名以save、insert、add、create、delete、upate开头时spring会自动帮我们开启事务(前提是我们配置了事务传播行为),而find、select、get开头的接口是查询,不涉及更改数据库,因此不需要事务,spring不会为查询接口自动开启事务。下面再说说切面,也就是事务的作用范围,execution(* com.taotao.service.*.*(..))的意思是,com.taotao.service下的任意类的任意方法的任意参数及任意返回值都是事务的切入点。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.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-4.2.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.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="add*" propagation="REQUIRED" />
- <tx:method name="create*" 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="select*" 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.taotao.service.*.*(..))" />
- </aop:config>
- </beans>
我们在服务层新建了三个配置文件,那么程序是怎么知道这三个文件的呢?这就需要在服务层初始化spring容器,方法是在taotao-manager-service工程下的web.xml文件中进行配置。web.xml文件所在的位置如下图所示。
web.xml文件的内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- id="WebApp_ID"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>taotao-manager</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 初始化spring容器 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/applicationContext-*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>