使用AspectJ时需要导入aspectjrt-1.8.13.jar和aspectjweaver-1.8.13.jar这两个jar包。
Annotation方式使用AOP
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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<context:component-scan base-package="com"/>
<aop:aspectj-autoproxy/>
</beans>
切入类定义
@Aspect //定义这个类为切入类
@Component
public class LoginInterceptorAnnotation {
@Before("doPoint()")
public void doQian(JoinPoint joinPoint) {
System.out.println("方法开始前我执行了!");
}
@After("execution(* com.samrtian.service.*.*(..))")
public void doHou() {
System.out.println("方法开始后我执行了!");
}
@Around("doPoint()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("前");
Object retVal = pjp.proceed();
System.out.println("后");
return retVal;
}
//定义切入哪些方法
@Pointcut("execution(* com.samrtian.service.*.*(..))")
public void doPoint() {}
}
@Aspect 定义类为切入类
@Pointcut 声明一个切入策略供 @Before @After @ 选择
@Before 被切入方法执行前执行
@After 被切入方法执行后执行
@Around 被切入方法前后都可以加入一些逻辑
@AfterReturning 被切入方法返回时执行
JoinPoint 加入这个参数可以获取被切入方法的名称和参数
XML方式使用AOP
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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<context:component-scan base-package="com"/>
<!-- <aop:aspectj-autoproxy/> -->
<bean id="loginInterceptorAnnotation" class="com.samrtian.aop.LoginInterceptorAnnotation"/>
<aop:config>
<aop:aspect id="logInterceptor" ref="loginInterceptorAnnotation">
<aop:around method="doAround" pointcut="execution(* com.samrtian.service.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
代码
public class LoginInterceptorAnnotation {
// @Before("doPoint()")
public void doQian(JoinPoint joinPoint) {
System.out.println("方法开始前我执行了!");
}
// @After("execution(* com.samrtian.service.*.*(..))")
public void doHou() {
System.out.println("方法开始后我执行了!");
}
// @Around("doPoint()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("前");
Object retVal = pjp.proceed();
System.out.println("后");
return retVal;
}
//定义切入哪些方法
@Pointcut("execution(* com.samrtian.service.*.*(..))")
public void doPoint() {}
}
基本上就这些了