within(com.service.RoleServiceImpl) | 指定一个类 |
---|---|
within(com.service.*) | 只包含当前目录下的类 |
within(com.service.*) | 包含当前目录及所有子目录下的类 |
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssttisme</groupId>
<artifactId>spring_aop_expression</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!--AOP的实现基于AspectJ框架 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
</project>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置组件扫描 -->
<context:component-scan base-package="com" />
<!--启用aop为bean创建动态代理对象-->
<aop:aspectj-autoproxy/>
</beans>
package com.service;
public interface RoleService {
void save(String role);
int update(String role);
}
package com.service.impl;
import org.springframework.stereotype.Component;
import com.service.RoleService;
@Component
public class RoleServiceImpl implements RoleService{
@Override
public void save(String role) {
System.out.println("save role "+role);
throw new RuntimeException("save exception");
}
@Override
public int update(String role) {
System.out.println("update role "+role);
return 1;
}
}
package com.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TxManager {
@Before("within(com.service.impl.*)")
public void beforeAdvice(){
System.out.println("tx->beforeAdvice");
}
/**
* 无论核心业务是否正常结束,这个通知都会执行,所以通常将此通知
* 理解为最终通知
*/
@After("within(com.service..*)")
public void afterAdvice(){
System.out.println("tx->afterAdvice");
}
/**
* 加入核心业务执行结束没有出现异常,会执行这个后置通知
*/
@AfterReturning("within(com.service.impl.RoleServiceImpl)")
public void afterReturningAdvice(){
System.out.println("tx->afterReturningAdvice");
}
/**
* 当核心业务方法出现异常时执行
*/
@AfterThrowing("within(com.service.impl.RoleServiceImpl)")
public void afterThrowingAdvice(){
System.out.println("tx->afterThrowingAdvice");
}
}
情况一(写法1):
package com.app;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.RoleService;
public class RunApp {
private static ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext();
//初始化
public static void init(){
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
}
//释放资源
public static void destory(){
ctx.close();
}
public static void main(String[] args) {
init();
RoleService roleService=ctx.getBean("roleServiceImpl",RoleService.class);
roleService.update("admin");
destory();
}
}
运行结果
tx->beforeAdvice
update role admin
tx->afterAdvice
tx->afterReturningAdvice
情况二(写法2):
tx->beforeAdvice
Exception in thread "main" save role admin
tx->afterAdvice
tx->afterThrowingAdvice
java.lang.RuntimeException: save exception
at com.service.impl.RoleServiceImpl.save(RoleServiceImpl.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy10.save(Unknown Source)
at com.app.RunApp.main(RunApp.java:20)
类比try-catcth
try {
@Before
核心业务
@AfterReturning
} catch (Exception e) {
@AfterThrowing
} finally {
@After
}
情况一是没有异常的执行顺序:
@Before ->核心业务->@After-> @AfterReturning
情况二是异常的执行顺序:
@Before ->核心业务->@After-> @AfterThrowing