【实现AOP的三种方式】

实现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("环绕后");
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值