1.切面面向编程AOP
第一步:导入切面相关联的坐标
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
第二步:在配置文件中追加切面的命名空间
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
第三步:注解版本开启切面自动代理
<!--开启切面注解-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
第四步:创建切面,标明注解
package com.zx.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//@Component
@Aspect
public class MyAspect {
/*
切面:
切入点
通知类型
前置通知@Before
后置通知@AfterReturning
最终通知@After
抛异常通知@AfterThrowing
环绕通知@Around
切入点表达式
语法:通知类型("execution(切入的方法)")
访问修饰符:一般是 public
返回值: void int String User ....
通配符:* 匹配所有返回值
包名: com.zx.servlet.* servlet包下面所有的类
com.zx.service.* 一般情况下切入到接口包下面即可 接口包中的所有接口
com.zx.service..* 接口包以及子包下面的所有
方法名称: 类全限定类名.方法名称
add* add开头的方法
*add add结尾的方法
* 所有方法
参数:
(int) (java.lang.String) (com.zx.entity.User)
(*) 一个任意类型的参数
(*,*) 两个任意类型的参数
(..) 任意个数任意类型的参数
*/
//切入点表达式
@Before("execution(public * com.zx.service..*.*(..))")
public void test01(){
System.out.println("前置通知....");
}
@AfterReturning("execution(public void com.zx.service.impl.UserServiceImpl.add())")
public void test02(){
System.out.println("后置通知....");
}
@After("execution(public void com.zx.service.impl.UserServiceImpl.add())")
public void test03(){
System.out.println("最终通知....");
}
@AfterThrowing("execution(public void com.zx.service.impl.UserServiceImpl.add())")
public void test04(){
System.out.println("抛异常通知....");
}
@Around("execution(public void com.zx.service.impl.UserServiceImpl.add())")
public void test05(ProceedingJoinPoint p) throws Throwable {
System.out.println("环绕通知....前");
p.proceed();
System.out.println("环绕通知....后");
}
}
或者采用XML版本配置切面
第三步:XML版本在beans.xml中配置切面
<!--aop相关XML配置-->
<bean id="myAspect_xml" class="com.zx.aop.MyAspect_XML"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="myAspect_xml">
<aop:pointcut id="pointcut" expression="execution(public * com.zx.service..*.*(..))"/>
<aop:before method="test01" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
2. 声明式事务管理
第一步:检查项目中是否已经有Spring-tx的jar包坐标
第二步:在beans.xml中配置tx的命名空间
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
第三步:注解版本事务管理,在beans.xml中配置事务管理器
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
第四步:在需要做事务的方法上边标明事务注解
@Transactional
void updateSal(int id1,int id2,double sal);