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;
        }
        
        
        
        
        

  • 相关阅读:
    Java:多线程
    javascript:正则表达式、一个表单验证的例子
    DOM对象和window对象
    javascript:面向对象和常见内置对象及操作
    如何检查CentOS服务器受到DDOS攻击
    CentOS防SYN攻击
    CentOS服务器简单判断CC攻击的命令
    在VMware中为CentOS配置静态ip并可访问网络
    安全运维之:网络实时流量监测工具iftop
    安全运维之:网络实时流量监测工具iftop
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2408509.html
Copyright © 2011-2022 走看看