基于AspectJ的Aop开发有两种:
第一种:使用xml开发
第二种:使用注解开发
案例综述:
在执行增删改方法时,执行Logger类,
是前置通知的,在执行增删改之前,执行前置通知
是后置通知的,在执行增删改之后,执行后置通知
是异常通知的,出现异常时,执行异常通知
是最终通知的,在执行增删改之后,执行最终通知
环绕通知一个“人”,能搞定上面四个“人”干的事情
准备工作
创建相应文件,如图
导入相关的依赖在pom.xml中
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
第一种:基于xml的前置通知,后置通知,异常通知,最终通知
编写IAccountService的接口文件 (所有的方法是连接点
Joinpoint,被增强的方法是切入点
Pointcut)
/**
* 账户的业务逻辑
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
/**
* 模拟更新账户
*/
void updateAccount(int i);
/**
* 模拟删除
*/
int deleteAccount();
}
AccountImpl.java的实体类
/**
* 业务层实现类
*/
public class AccountImpl implements IAccountService {
public void saveAccount() {
System.out.println("保存");
}
public void updateAccount(int i) {
System.out.println("更新");
}
public int deleteAccount() {
System.out.println("删除");
return 0;
}
}
Logger.java
/**
1. 用于打印日志,计划让其切入点方法执行(切入点就是业务层方法)
*/
public class Logger {
//前置通知
public void beforPringLog(){
System.out.println("Logger类中,前置开始记录了。。。");
}
//后置通知
public void afterreturnPringLog(){
System.out.println("Logger类中,后置开始记录了。。。");
}
//异常通知
public void afterthrowingpringLog(){
System.out.println("Logger类中,异常开始记录了。。。");
}
//最终通知
public void afterPringLog(){
System.out.println("Logger类中,最终开始记录了。。。");
}
//环绕通知
public Object aroundPringLog(ProceedingJoinPoint pjp){
Object revalue =null;
try {
Object[] args = pjp.getArgs();//得到方法执行所需要的参数
System.out.println("环绕前置通知。。。。。");
revalue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println("环绕后置通知。。。。。");
} catch (Throwable throwable) {
System.out.println("环绕异常通知。。。。。");
throwable.printStackTrace();
}finally {
System.out.println("环绕最终通知。。。。。");
}
return revalue;
}
}
**配置文件bean.xml
- 把通知bean也交给spring来管理
- 使用aop:config标签表明开始AOP的配置
- 使用aop:aspect标签表明配置切面
id属性:给切面提供一个唯一标识
ref属性:是给指定通知类bean的id - 在aop:aspect标签内部使用对应的标签来配置通知的类型
aop:before 表示前置通知
**method:**用于指定通知类中的那个方法是前置通知
**pointcut:**用于指定切入点表达式,该表达式的含义指得是业务层的那些方法增强
切入点表达式规则
1.访问修饰符 返回值 包名.包名....类名.方法名(参数列表)
例:只对service中的saveAccount方法进行了增强
pointcut="execution(public void com.itheima.service.impl.AccountImpl.saveAccount())
2.访问修饰符 可以省略
pointcut="execution(void com.itheima.service.impl.AccountImpl.saveAccount())
3.返回值可以使用通配符,表示任意返回值
pointcut="execution(* com.itheima.service.impl.AccountImpl.saveAccount())
4.包名可以使用通配符,有几级包,就需要几个*
pointcut="execution(* *.*.*.*.AccountImpl.saveAccount())
5.类名也可以使用通配符
pointcut="execution(* *.*..*.saveAccount())
6.类名和方法名也可以使用通配符
a.这个表示service 中不带参数的所有方法都增强了
pointcut="execution(* *.*..*.*())
b.这个表示service 中带参数的所有方法都增强了
pointcut="execution(* *.*..*.*(*))
c.这个表示service 中所有方法都增强了
pointcut="execution(* *.*..*.*(..))
实际开发中常用方法:切到业务实现类下的所有方法
pointcut="execution(* com.itheima.service.impl.*.*(..))
最终写出来的bean.xml
<?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">
<!--配置spring的ioc,把service对象配置进去-->
<bean id="accountService" class="com.itheima.service.impl.AccountImpl"></bean>
<!--配置logger类-->
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<!-- 配置AOP-->
<aop:config>
<!--配置切入点表达式, id 属性用于指定表达式的唯一标识,expression指定表达式的内容-->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置前置通知,在切入方法执行之前执行-->
<aop:before method="beforPringLog" pointcut-ref="pt1"></aop:before>
<!-- 配置后置通知,在切入方法正常执行后执行,和异常永远只能执行一次-->
<aop:after-returning method="afterreturnPringLog" pointcut-ref="pt1"></aop:after-returning>
<!-- 配置异常通知,在切入方法产生异常后执行,和后置永远只能执行一次-->
<aop:after-throwing method="afterthrowingpringLog" pointcut-ref="pt1"></aop:after-throwing>
<!-- 配置最终通知,在切入方法是否正常执行后都会执行-->
<aop:after method="afterPringLog" pointcut-ref="pt1"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试类
public class AOPtest {
public static void main(String[] args) {
//获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
IAccountService as = (IAccountService)ac.getBean("accountService");
//执行方法
as.saveAccount();
as.deleteAccount();
as.updateAccount(1);
}
}
运行的效果
因为切入点表达式配置的是IAccountImpl下的所有方法,所以logger类会在每个方法下都会执行。
同时异常通知只有出现异常时才会出现,并且异常通知和最后通知只能出来一个
细心的朋友发现了我在logger中配置的环绕没有用到,那是因为前置通知,后置通知,最终通知,异常通知不能和环绕通知一起出现。
使用环绕通知只需要改bean.xml了
<?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">
<!--配置spring的ioc,把service对象配置进去-->
<bean id="accountService" class="com.itheima.service.impl.AccountImpl"></bean>
<!--配置logger类-->
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<!-- 配置AOP-->
<aop:config>
<!--配置切入点表达式, id 属性用于指定表达式的唯一标识,expression指定表达式的内容-->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!--配置环绕通知-->
<aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
</beans>
测试的结果也是OK的
第二种:基于注解的开发
建议将上面的项目重新复制一份,然后在上面代码的基础上进行更改即可,目录结构一模一样,一点小改动就是我为了命名规范,我把文件上面AccountImpl改为了AccountServiceImpl
下面开始在第一个方法的代码上进行更改
第一步: 在AccountImpl实体类的方法上加上注解@Service(“accountService”)
第二步: 把bean.xml中多余的去掉,并且
配置spring创建容器时要扫描的包
和配置spring开启注解AOP的支持
<?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">
<!-- 配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
第三步: 改写Logger.java
这里要注意了还是上面提到问题,
前置通知 @Before("pt1()")
,后置通知@AfterReturning("pt1()"),
最终通知 @After("pt1()")
,异常通知@AfterThrowing("pt1()")
不能和环绕通知@Around("pt1()")
一起出现。
我下面的代码是将@Around("pt1()")
给注释掉了,也就是没用环绕通知。
若是想用环绕通知,需要将前置,后置,最终,异常通知的注解 用 // 将他们注释掉,环绕通知的注解,不进行注释
/**
* 用于打印日志,计划让其切
*/
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1(){}
@Before("pt1()")
public void beforPringLog(){
System.out.println("Logger类中,前置开始记录了。。。");
}
@AfterReturning("pt1()")
public void afterreturnPringLog(){
System.out.println("Logger类中,后置开始记录了。。。");
}
@AfterThrowing("pt1()")
public void afterthrowingpringLog(){
System.out.println("Logger类中,异常开始记录了。。。");
}
@After("pt1()")
public void afterPringLog(){
System.out.println("Logger类中,最终开始记录了。。。");
}
//@Around("pt1()") //要注意这里哦!我将环绕通知注释掉了
public Object aroundPringLog(ProceedingJoinPoint pjp){
Object revalue =null;
try {
Object[] args = pjp.getArgs();//得到方法执行所需要的参数
System.out.println("环绕前置通知。。。。。");
revalue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println("环绕后置通知。。。。。");
} catch (Throwable throwable) {
System.out.println("环绕异常通知。。。。。");
throwable.printStackTrace();
}finally {
System.out.println("环绕最终通知。。。。。");
}
return revalue;
}
}
测试类不变,测试结果
使用环绕通知,结果
到此基于AspectJ的Aop开发案例,两种方法全部介绍完毕。如有问题及时联系,我会及时更正!!欢迎大佬指教!!