SpringAOP原生实现接口的使用

需要的包

Maven 依赖

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>

定义拦截四个接口

前置拦截继承         MethodBeforeAdvice

环绕拦截继承        MethodInterceptor

后置拦截继承        AfterReturningAdvice

异常拦截继承        ThrowsAdvice 

 方式一:sping-AP接口

 不需要切面,只需要定义一个类,实现接口。就知道在哪执行了

//前置拦截通知
public class log implements MethodBeforeAdvice {

    /**
     *
     * @param method  要执行的目标对象方法
     * @param args      参数
     * @param target    目标对象
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable 
    {

        System.out.println(target.getClass().getName()+"的 " +method.getName()+"被执行了");
    }
}
//后置拦截通知
public class AfterLog implements AfterReturningAdvice 
{
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable 
    {
        System.out.println("执行了"+method.getName()+"方法的返回结果"+returnValue);
    }
}
//环绕拦截通知
public class Befor implements MethodInterceptor 
{
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable 
    {
        System.out.println("环绕");
        return null;
    }
}

    <!--被执行的实现类-->
    <bean id="use" class="com.kjc.service.serviceimpl.Serviceimpl"/>
    <!--AOP实现接口的通知类-->
    <bean id="log" class="com.kjc.Log.log"/>
    <bean id="after" class="com.kjc.Log.AfterLog"/>
    <bean id="befor" class="com.kjc.Log.Befor"/>
    <aop:config>
        <aop:pointcut id="point" expression="execution(* com.kjc.service.serviceimpl.Serviceimpl.*(..))"/>

        <aop:advisor advice-ref="log" pointcut-ref="point"/>
        <aop:advisor advice-ref="after" pointcut-ref="point"/>
        <aop:advisor advice-ref="befor" pointcut-ref="point"/>
    </aop:config>

方式二:自定义通知类

//自定义通知类
public class loginform {
    
    public void before()
    {
        System.out.println("before方法...");
    }
    public void after()
    {
        System.out.println("after方法...");
    }

}
    <!--被执行的实现类-->
    <bean id="use" class="com.kjc.service.serviceimpl.Serviceimpl"/>

    <!--自定义的AOP通知类-->
    <bean id="loginform" class="com.kjc.Log.loginform"/>

    <aop:config>
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* com.kjc.service.serviceimpl.Serviceimpl.*(..))"/>

        <!--aop:aspect  自定义切面 ref 引用的自定义通知类-->
        <aop:aspect ref="loginform">
            <!--mtehod 自定义通知类 中 的方法  pointcut-ref 切入点-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>

    </aop:config>

 方式三:注解实现


//注解实现AOP
@Aspect //标记这个类是一个 切面
public class CommentAOP 
{

    @Before("execution(* com.kjc.service.serviceimpl.Serviceimpl.*(..))")
    public void before()
    {
        System.out.println("before...");
    }



    @After("execution(* com.kjc.service.serviceimpl.Serviceimpl.*(..))")
    public void after()
    {
        System.out.println("after.......");
    }

}
   <!--被执行的实现类-->
    <bean id="use" class="com.kjc.service.serviceimpl.Serviceimpl"/>

    <!--注册到  bean ,如果不注册bean也可以直接使用注解@-->
    <bean id="commentAOP" class="com.kjc.Log.CommentAOP"/>
    <!--开启注解-->
    <aop:aspectj-autoproxy/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一剑封喉の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值