专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 三种AOP的实现方式

三种AOP的实现方式

更新时间:2022-10-26 09:33:13 来源:赢咖4 浏览776次

AOP的实现方式有三种:

1.使用Spring的API接口(主要是SpringAPI接口实现)

2.自定义实现AOP(主要是切面定义,自定义类)

3.使用注解实现

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--    <bean id="user" class="com.bing.pojo.User" p:name="cb" p:age="23"></bean>-->
<!--注册成bean-->
    <bean id="UserService" class="com.bing.service.UserServiceImpl"></bean>
    <bean id="log" class="com.bing.log.Log"></bean>
    <bean id="afterlog" class="com.bing.log.AfterLog"></bean>
<!--&lt;!&ndash;    方式一:使用原生spring API接口&ndash;&gt;-->
<!--&lt;!&ndash;    配置aop&ndash;&gt;-->
<!--    <aop:config>-->
<!--&lt;!&ndash;        需要一个切入点,即我们需要在哪个地方执行方法 exe: 返回值 类名 方法名 参数&ndash;&gt;-->
<!--        <aop:pointcut id="pointcut" expression="execution(* com.bing.service.UserServiceImpl.*(..))"/>-->
<!--        &lt;!&ndash;    执行环绕增加&ndash;&gt;-->
<!--&lt;!&ndash;把log类切入到pointcut方法上面&ndash;&gt;-->
<!--        <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor>-->
<!--        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"></aop:advisor>-->
<!--    </aop:config>-->
<!--    方式二 自定义类 用切面,是一个类-->
<!--    <bean id="diy" class="com.bing.diy.DiyPointCut"/>-->
<!--         <aop:config>-->
<!--&lt;!&ndash;             自定义切面, 引入diy类&ndash;&gt;-->
<!--              <aop:aspect ref="diy" >-->
<!--                  <aop:pointcut id="point" expression="execution(* com.bing.service.UserServiceImpl.*(..))"/>-->
<!--                  &lt;!&ndash;有切面类就有通知了,就是有方法了&ndash;&gt;-->
<!--                  <aop:before method="before" pointcut-ref="point"/>-->
<!--                  <aop:after method="after" pointcut-ref="point"/>-->
<!--              </aop:aspect>-->
<!--         </aop:config>-->
<!--    方式三-->
    <bean id="annotationPointCut" class="com.bing.diy.AnnotationPointCut"></bean>
<!--    开启注解支持 自动代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
//方法一、二
package com.bing.diy;
public class DiyPointCut {
    public void before(){
        System.out.println("执行前");
    }
    public void after(){
        System.out.println("执行后");
    }
}
//方法三(注解)
package com.bing.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解直接将其类标记成切面
@Aspect
public class AnnotationPointCut {
//Before里面写切入点
    @Before("execution(* com.bing.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前");
    }
    @After("execution(* com.bing.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要处理切入的点
    @Around("execution(* com.bing.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前");
        //执行方法,过滤
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后 ");
    }
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>