zoukankan      html  css  js  c++  java
  • Spring Aop支持的两种方式

    1.xml配置方式,需要将myInterceptor类中的注解去掉

    <?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:context
    ="http://www.springframework.org/schema/context" 
           xmlns:aop
    ="http://www.springframework.org/schema/aop"      
           xsi:schemaLocation
    ="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    >
            <aop:aspectj-autoproxy/> 
            <bean id="personService" class="com.westsoft.serviceimpl.PersonServiceBean"></bean>
            <bean id="aspetbean" class="com.westsoft.serviceimpl.MyInterceptor"/>
            <aop:config>
                <aop:aspect id="asp" ref="aspetbean">
                    <aop:pointcut id="mycut" expression="execution(* com.westsoft.serviceimpl.PersonServiceBean.*(..))"/>
                    <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
                    <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
                      <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
                      <aop:after pointcut-ref="mycut" method="doAfter"/>
                      <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
                </aop:aspect>
            </aop:config>

    </beans> 

    2.注解方式

    package com.westsoft.serviceimpl;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Service;

    @Aspect @Service
    public class MyInterceptor {

        /*
         * 切入点
         
    */
        @Pointcut("execution(* com.westsoft.serviceimpl.PersonServiceBean.*(..))")
        private void anyMethod(){}
        
        /*
         * 传入参数
         
    */
        @Before("anyMethod() && args(name)")
        public void doSomthind(String name)
        {
            System.out.println("前置通知:"+name);
        }
        
        
        /*
         * returning="result" 返回结果
         
    */
        @AfterReturning(pointcut="anyMethod()",returning="result")
        public void doAfterreturning(String result)
        {
            System.out.println("后置通知"+result);
        }
        
        /*
         * 获取例外
         
    */
        @AfterThrowing(pointcut="anyMethod()",throwing="e")
        public void doAftertrhrowing(Exception e)
        {
            System.out.println("例外通知"+e);
        }
        
        @After("anyMethod()")
        public void doAfter()
        {
            System.out.println("最终通知");
        }
        
        @Around("anyMethod()")
        public Object doAround(ProceedingJoinPoint pjp) throws Throwable
        {
            System.out.println("环绕通知");
            Object rst=pjp.proceed();
            System.out.println("环绕通知");
            return rst;
        }
        
        
        
        
        

  • 相关阅读:
    switch语句(上)(转载)
    MSIL实用指南-生成if...else...语句
    Docker 部署Jira8.1.0
    K8S从入门到放弃系列-(13)Kubernetes集群mertics-server部署
    K8S踩坑篇-master节点作为node节点加入集群
    K8S从入门到放弃系列-(12)Kubernetes集群Coredns部署
    K8S从入门到放弃系列-(11)kubernetes集群网络Calico部署
    K8S从入门到放弃系列-(10)kubernetes集群之kube-proxy部署
    K8S从入门到放弃系列-(9)kubernetes集群之kubelet部署
    K8S从入门到放弃系列-(8)kube-apiserver 高可用配置
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2408509.html
Copyright © 2011-2022 走看看