基于XML的AOP配置
环境搭建
第一步:准备客户的业务层和接口(需要增强的类)
/**
* 客户的业务层接口
*/
public interface ICustomerService {
/**
* 保存客户
*/
void saveCustomer();
/**
* 修改客户
* @param i
*/
void updateCustomer(int i);
}
/**
* 客户的业务层实现类
*/
public class CustomerServiceImpl implements ICustomerService {
@Override
public void saveCustomer() {
System.out.println("调用持久层,执行保存客户");
}
@Override
public void updateCustomer(int i) {
System.out.println("调用持久层,执行修改客户");
}
}
第二步:拷贝必备的jar包到工程的lib目录
第三步:创建spring的配置文件并导入约束
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
第四步:把客户的业务层配置到spring容器中
<!-- 把资源交给spring来管理 -->
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"/>
第五步:制作通知(增强的类)
/**
* 一个记录日志的工具类
*/
public class Logger {
/**
* 期望:此方法在业务核心方法执行之前,就记录日志
*/
public void beforePrintLog(){
System.out.println("Logger类中的printLog方法开始记录日志了。。。。");
}
}
配置步骤
第一步:把通知类用bean标签配置起来
<!-- 把有公共代码的类也让spring来管理(把通知类也交给spring来管理) -->
<bean id="logger" class="com.itheima.util.Logger"></bean>
第二步:使用aop:config声明aop配置
<!-- aop的配置 -->
<aop:config>
<!-- 配置的代码都写在此处 -->
</aop:config>
第三步:使用aop:aspect配置切面
<!-- 配置切面 :此标签要出现在aop:config内部
id:给切面提供一个唯一标识
ref:引用的是通知类的bean的id
-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型要写在此处-->
</aop:aspect>
第四步:使用aop:before配置前置通知
<!-- 用于配置前置通知:指定增强的方法在切入点方法之前执行
method:用于指定通知类中的增强方法名称
ponitcut-ref:用于指定切入点的表达式的引用
-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"/>
第五步:使用aop:pointcut配置切入点表达式
<aop:pointcut expression="execution(public void com.itheima.service.impl.CustomerServiceImpl.saveCustomer())"
id="pt1"/>
切入点表达式说明 :
execution:
匹配方法的执行(常用)
execution(表达式)
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
全匹配方式:
public void com.itheima.service.impl.CustomerServiceImpl.saveCustomer()
访问修饰符可以省略
void com.itheima.service.impl.CustomerServiceImpl.saveCustomer()
返回值可以使用*号,表示任意返回值
* com.itheima.service.impl.CustomerServiceImpl.saveCustomer()
包名可以使用*号,表示任意包,但是有几级包,需要写几个*
* *.*.*.*.CustomerServiceImpl.saveCustomer()
使用..来表示当前包,及其子包
* com..CustomerServiceImpl.saveCustomer()
类名可以使用*号,表示任意类
* com..*.saveCustomer()
方法名可以使用*号,表示任意方法
* com..*.*()
参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
* com..*.*(*)
参数列表可以使用..表示有无参数均可,有参数可以是任意类型
* com..*.*(..)
全通配方式:
* *..*.*(..)
常用标签
<aop:config>
作用:
用于声明开始aop的配置
<aop:aspect>
作用:
用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类bean的id。
<aop:pointcut>
作用:
用于配置切入点表达式
属性:
expression:用于定义切入点表达式。
id:用于给切入点表达式提供一个唯一标识。
<aop:before>
作用:
用于配置前置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
<aop:after-returning>
作用:
用于配置后置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
<aop:after-throwing>
作用:
用于配置异常通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
<aop:after>
作用:
用于配置最终通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
<aop:around>
作用:
用于配置环绕通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
通知的类型
类型说明
<!-- 配置通知的类型
aop:before:
用于配置前置通知。前置通知的执行时间点:切入点方法执行之前执行
aop:after-returning:
用于配置后置通知。后置通知的执行时间点:切入点方法正常执行之后。它和异常通知只能有一个执行
aop:after-throwing
用于配置异常通知。异常通知的执行时间点:切入点方法执行产生异常后执行。它和后置通知只能执行一个。
aop:after
用于配置最终通知。最终通知的执行时间点:无论切入点方法执行时是否有异常,它都会在其后面执行。
aop:around
用于配置环绕通知。他和前面四个不一样,他不是用于指定通知方法何时执行的。
-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"/>
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/>
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"/>
<aop:after method="afterPrintLog" pointcut-ref="pt1"/>
<aop:around method="aroundPringLog" pointcut-ref="pt1"/>
环绕通知的特殊说明
/**
* 环绕通知
* 它是spring框架为我们提供的一种可以在代码中手动控制增强部分什么时候执行的方式。
* 问题:
* 当我们配置了环绕通知之后,增强的代码执行了,业务核心方法没有执行。
* 分析:
* 通过动态代理我们知道在invoke方法中,有明确调用业务核心方法:method.invoke()。
* 我们配置的环绕通知中,没有明确调用业务核心方法。
* 解决:
* spring框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数
* 在环绕通知执行时,spring框架会为我们提供该接口的实现类对象,我们直接使用就行。
* 该接口中有一个方法proceed(),此方法就相当于method.invoke()
*/
public void aroundPringLog(ProceedingJoinPoint pjp){
try {
System.out.println("前置通知:Logger类的aroundPringLog方法记录日志");
pjp.proceed();
System.out.println("后置通知:Logger类的aroundPringLog方法记录日志");
} catch (Throwable e) {
System.out.println("异常通知:Logger类的aroundPringLog方法记录日志");
e.printStackTrace();
}finally{
System.out.println("最终通知:Logger类的aroundPringLog方法记录日志");
}
}
基于注解的AOP配置
环境搭建
第一步:准备客户的业务层和接口并用注解配置(需要增强的类)
/**
* 客户的业务层接口
*/
public interface ICustomerService {
/**
* 保存客户
*/
void saveCustomer();
/**
* 修改客户
* @param i
*/
void updateCustomer(int i);
}
/**
* 客户的业务层实现类
*/
public class CustomerServiceImpl implements ICustomerService {
@Override
public void saveCustomer() {
System.out.println("调用持久层,执行保存客户");
}
@Override
public void updateCustomer(int i) {
System.out.println("调用持久层,执行修改客户");
}
}
第二步:拷贝必备的jar包到工程的lib目录
第三步:创建spring的配置文件并导入约束
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
第四步:把资源使用注解让spring来管理
/**
* 客户的业务层实现类
*/
@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
@Override
public void saveCustomer() {
System.out.println("调用持久层,执行保存客户");
}
@Override
public void updateCustomer(int i) {
System.out.println("调用持久层,执行修改客户");
}
}
第五步:在配置文件中指定spring要扫描的包
<!-- 告知spring,在创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
配置步骤
第一步:把通知类也使用注解配置
/**
* 一个记录日志的工具类
*/
@Component("logger")
public class Logger {
/**
* 期望:此方法在业务核心方法执行之前,就记录日志
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");
}
}
第二步:在通知类上使用@Aspect注解声明为切面
/**
* 一个记录日志的工具类
*/
@Component("logger")
@Aspect//表明当前类是一个切面类
public class Logger {
/**
* 期望:此方法在业务核心方法执行之前,就记录日志
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");
}
}
第三步:在增强的方法上使用@Before注解配置前置通知
/**
* 期望:此方法在业务核心方法执行之前,就记录日志
* 前置通知
*/
@Before("execution(* com.itheima.service.impl.*.*(..))")//表示前置通知
public void beforePrintLog(){
System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");
}
第四步:在spring配置文件中开启spring对注解AOP的支持
<!-- 开启spring对注解AOP的支持 -->
<aop:aspectj-autoproxy/>
常用注解
@Aspect:
作用:
把当前类声明为切面类。
@Before:
作用:
把当前方法看成是前置通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@AfterReturning
作用:
把当前方法看成是后置通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@AfterThrowing
作用:
把当前方法看成是异常通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@After
作用:
把当前方法看成是最终通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@Around
作用:
把当前方法看成是环绕通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@Pointcut
作用:
指定切入点表达式
属性:
value:指定表达式的内容
不使用XML的配置方式
@Configuration
@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}