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>
    

      

  • 相关阅读:
    序列JSON数据和四种AJAX操作方式
    jquery.validate和jquery.form.js实现表单提交
    JQuery Validate使用总结1:
    HOWTO: Include Base64 Encoded Binary Image Data (data URI scheme) in Inline Cascading Style Sheets (CSS)(转)
    SharePoint 2007 使用4.0 .Net
    动态IP解决方案
    取MS CRM表单的URL
    从Iframe或新开的窗口访问MS CRM 2011(转)
    Toggle or Hidden MS CRM Tab
    Windows 2008下修改域用户密码
  • 原文地址:https://www.cnblogs.com/E-star/p/3560920.html
Copyright © 2011-2022 走看看