什么是AspectJ?
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
Aspect注解版
AspectJ自动代理
1.在xml文件中配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.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">
<!--目标类型-->
<bean id="service" class="cn.happy.Aspect01.UserServiceImpl"></bean>
<!--增强-->
<bean id="MyAspect" class="cn.happy.Aspect01.UserTest"></bean>
<!--Aspectj自动代理-->
<aop:aspectj-autoproxy/>
</beans>
2.创建接口、类
package cn.happy.Aspect01;
/**
* Created by Administrator on 2018/3/10.
*/
public interface IUserService {
void select();
void update();
}
package cn.happy.Aspect01;
/**
* Created by Administrator on 2018/3/10.
*/
public class UserServiceImpl implements IUserService {
public void select() {
System.out.println("select OK!");
}
public void update() {
int i=5/0;
System.out.println("update OK!");
}
}
UserTest类
package cn.happy.Aspect01;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* Created by Administrator on 2018/3/10.
*/
@Aspect
public class UserTest {
//前置增强
/*@Before("execution(* *..Aspect01.*.select(..))")
public void MyAspectBefore(){
System.out.println("Before===");
}*/
//后置增强
/*@AfterReturning("execution(* *..Aspect01.*.select(..))")
public void MyAspectAfter(){
System.out.println("After===");
}*/
/*//环绕增强
@Around("execution(* *..Aspect01.*.select(..))")
public void MyAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕增强A");
pjp.proceed();
System.out.println("环绕增强B");
}*/
/*//异常增强
@AfterThrowing("execution(* *..Aspect01.*.update(..))")
public void MyAfterThrowing(){
System.out.println("网络异常");
}*/
//最终增强
@After("execution(* *..Aspect01.*.update(..))")
public void MyAfter(){
System.out.println("最终增强");
}
//PointCut注解
@Pointcut(value="execution(* *..Aspect01.*.select(..))")
public void selects(){}
@Pointcut(value="execution(* *..Aspect01.*.update(..))")
public void update(){}
@AfterReturning("selects()||update()")
public void MyPointcutAfter(){
System.out.println("After===");
}
}
AspectJ XML版
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.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">
<!--目标类型-->
<bean id="service" class="cn.happy.Aspect02.IUserServiceImpl"></bean>
<!--增强-->
<bean id="MyAspect" class="cn.happy.Aspect02.UserTest"></bean>
<aop:config>
<!--切点-->
<aop:pointcut id="MyPointCut" expression="execution(* *..Aspect02.*.select(..))"/>
<!--切面Aspect-->
<aop:aspect ref="MyAspect">
<!--前置增强-->
<aop:before method="select" pointcut-ref="MyPointCut"/>
<!--后置增强-->
<aop:after-returning method="After" pointcut-ref="MyPointCut"/>
<!--环绕增强-->
<aop:around method="MyAround" pointcut-ref="MyPointCut"/>
<!--异常增强-->
<aop:after-throwing method="MyThrowing" pointcut-ref="MyPointCut"/>
<!--<!–最终增强–>
<aop:after method="MyAfter" pointcut-ref="MyPointCut"/>-->
</aop:aspect>
</aop:config>
</beans>