zoukankan      html  css  js  c++  java
  • Spring注解实现AOP

     

    ------------------------------------------

    要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:

    <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-2.5.xsd

               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    </beans>

    Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

    基于XML配置方式声明切面。

    基于注解方式声明切面

    自定的切面:(注意我们的切面一定要交给Spring管理才行,也即在XML文件里配置)

    package cn.gbx.example;
    
    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;
    
    @Aspect
    public class MyIntercept {
    
    	@Pointcut("execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))")
    	private void anyMethod() {} // 定义一个切入点
    	
    	
    	@Before("anyMethod() && args(name)")  //利用arg来限定拥有一个参数, 参数的名字是String类型,然后得到该参数
    	public void doAccessCheck(String name) {
    		System.out.println("Name = " + name);
    		System.out.println("前置通知");
    	}
    	
    	@AfterReturning(pointcut = "anyMethod()", returning="name") //获得返回结果
    	public void doAfterReturning(String name) {
    		System.out.println("返回Name = " + name);
    		System.out.println("后置通知");
    	}
    	
    	@After("anyMethod()")
    	public void doAfter() {
    		System.out.println("最终通知");
    	}
    	
    	@AfterThrowing(pointcut = "anyMethod()", throwing="e")
    	public void doAfterThrowing(Exception e) {
    		System.out.println(e.getMessage());
    		System.out.println("例外通知");
    	}
    	
    	@Around("anyMethod()")//环绕通知  可以在环绕通知里面坐权限的限定 struts 的连接器就是  定义格式是固定的
    	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
    		System.out.println("进入方法");
    		Object value = pjp.proceed();
    		System.out.println("退出方法");
    		return value;
    	}
    }
    
    	
    

      

    XML配置文件:

    <?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: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-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="cn.gbx.serviceimpl.PersonServiceImpl"></bean>
            
             <bean id="myIntercept" class="cn.gbx.example.MyIntercept"></bean>
    </beans>
    

      

  • 相关阅读:
    c# GDI+简单绘图(二)
    ProE 工程图教程系列4 ProE不能导出dwg等格式的解决办法
    McAfee卸载工具及卡巴KIS2009注册码
    Vista下修改网卡MAC地址
    开机后出现Spooler Subsystem App 的解决办法
    微软office 2007在线编辑平台Office Live Workspace(docx转doc格式)
    美丽的草原风电场————内蒙古锡林浩特阿巴嘎旗风电场
    ProE官方网站系列视频教程
    [转]Stimator:评估您的网站/博客的价值
    关注苏迪曼杯,关注Lining为羽毛球队打造的衣服
  • 原文地址:https://www.cnblogs.com/E-star/p/3560920.html
Copyright © 2011-2022 走看看