实现AOP的三种方式
首先导入依赖包:aspecyjweaver 和aop约束
名词解释
pointcut:切入点
aspect:切面(修饰类)
advice:通知(修饰方法)
方式一:试用Spring的API接口
<aop:config>
<aop:pointcut id="名" expression="execution(* 包.类.*(..))"> <!--切入点-->
<aop:advisor advice-ref=类 pointcut=点> <!--通知-->
</aop:config>
方式二:自定义类(切面)
<aop:config>
<aop:aspect ref=类>
<aop:pointcut id="名" expression="execution(* 包.类.*(..))">
<aop:before method=方法 pointcut-ref=点>
</aop:aspect>
</aop:config>
方式三:使用注解(需要注册bean、开启注解支持)
@Aspect
public class xxx{
@Before("execution(* 包.类.*(..))")
public void before(){
System.out.println();
}
}
-----------------------------------分割线------------------------------------------
详细代码:
-
结构
-
applicationContext.xml
<!--注册bean-->
<bean id="afterLog" class="com.zane.log.AfterLog"/>
<bean id="beforeLog" class="com.zane.log.BeforeLog"/>
<bean id="userService" class="com.zane.service.UserServiceImp"/>
<!--方式一:使用spring接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:execution:表达式,execution(要执行的位置!* * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.zane.service.UserServiceImp.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<!--<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
</aop:config>
<!--方式二:自定义类-->
<!--注册bean-->
<bean id="diy" class="com.zane.diy.DiyAspect"/>
<aop:config>
<!--切面:针对类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.zane.service.UserServiceImp.*(..))"/>
<!--通知:针对方法-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
<!--方式三:注解-->
<bean id="annotationPointCut" class="com.zane.diy.AnnotationPointCut"/>
<!--开启注解支持!-->
<aop:aspectj-autoproxy/>
- UserService
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
- UserServiceImp
public class UserServiceImp implements UserService{
public void add() {
System.out.println("添加");
}
public void delete() {
System.out.println("删除");
}
public void update() {
System.out.println("添加");
}
public void select() {
System.out.println("添加");
}
}
- BeforeLog
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
- AfterLog
public class AfterLog implements AfterReturningAdvice {
//比前置多了一个returnValue
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getClass().getName()+"的"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
- MyTest
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口,重要
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
- DiyAspect(第二种方式)–推荐
public class DiyAspect {
public void before(){
System.out.println("=========方法之前执行=========");
}
public void after(){
System.out.println("=========方法之后执行=========");
}
}
- AnnotationPointCut
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.zane.service.UserServiceImp.*(..))")
public void before(){
System.out.println("=======方法前执行=======");
}
@After("execution(* com.zane.service.UserServiceImp.*(..))")
public void after(){
System.out.println("=======方法后执行=======");
}
// @Around("execution(* com.zane.service.UserServiceImp.*(..))")
// public void around(ProceedingJoinPoint jp) throws Throwable{
// System.out.println("环绕前");
// Object proceed = jp.proceed();
// System.out.println("环绕后");
// }
}