当前位置: 移动技术网 > IT编程>开发语言>Java > 基于注解的AOP配置

基于注解的AOP配置

2020年04月18日  | 移动技术网IT编程  | 我要评论

基于注解的aop配置(一定要要参考上一篇的“基于xml文件的aop配置”)

  • 主要是将上面的xml文件的内容替换掉,以下是xml文件所有内容,环绕通知的内容就是替换四种通知即可

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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">
        
        <bean id="wordsservice" class="com.mypro.service.impl.wordsserviceimpl"></bean>
        
        <bean id="logger" class="com.mypro.service.impl.wordsserviceimpl"></bean>
    <aop:config>
        <aop:aspect id="loggeradvice" ref="logger">
            <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:point>
            <aop:before method="beforeinfo" pointcut-ref="pt1"></aop:before>
            <aop:after-returning method="afterreturninginfo" pointcut-ref="pt1"></aop:after-returning>
            <aop:after-throwing method="afterthrowinginfo" pointcut-ref="pt1"></aop:after-throwing>
            <aop:after method="afterinfo" pointcut-ref="pt1"></aop:after>
        </aop:aspect>
    </aop:config>
    </beans>
  • 首先我们需要配置我们扫描的包,将xml文件的bean对象替换成注解,即在我们实现类上方添加注解。在此之前,还需要添加xmlns:context依赖

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
        <!-- 代替xml文件的两个bean对象的配置 -->
        <context:component-scan base-package="com.mypro"></context:component-scan>
    </beans>
  • 开启spring支持的aop配置

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  • 修改通知类的代码,注意实现五种通知标签的功能

    package com.mypro.utils;
    ​
    import org.aspectj.lang.proceedingjoinpoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.component;
    ​
    /**
     * 用于记录日志的工具类,里面提供公共的代码
     */
    // 代替xml文件的<aop:config><aop:aspect id="loggeradvice" ref="logger"></aop:aspect></aop:config>
    @component("logger")
    @aspect                      
    public class logger {
        
        // 代替切入点的配置
        @pointcut("execution(* com.mypro.service.impl.*.*(..))")
        public void pt1(){}
    ​
        @before("pt1()")
        public void beforeinfo(){
            system.out.println("前置通知");
        }
        @afterreturning("pt1()")
        public void totalinfo(){
            system.out.println("后置通知");
        }
        @afterthrowing("pt1()")
        public void expectinfo(){
            system.out.println("异常通知");
        }
        @after("pt1()")
        public void afterinfo(){
            system.out.println("最终通知");
        }
        
        @around("pt1()")
        public object aroundinfo(){
            object rtvalue = null;
            try{
                system.out.println("前置通知");
                // 得到方法执行时所需的参数
                object[] args = pjp.getargs();
                // 明确调用业务层方法(切入点方法)
                rtvalue = pjp.proceed(args);
                system.out.println("后置通知");
                return rtvalue;
            }catch(throwable t){      // 必须是用throwable捕捉异常
                system.out.println("异常通知");
                throw new runtimeexception(t);
            }finally{
                system.out.println("最终通知");
            }
        }
    ​
    }
    ​

     

     

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网