`
duyouhua1214
  • 浏览: 228215 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring AOP的两种实现方式

    博客分类:
  • Java
阅读更多

来源:http://javacrazyer.iteye.com/blog/794143

AOP常用实现方式是一个采用声明的方式来实现,一个采用注解的方式来实现,

温习下一些概念

   Joinpoint(连接点):程序执行时的某个特定的点,在Spring中就是某一个方法的执行
    Pointcut(切点):说的通俗点,spring中AOP的切点就是指一些方法的集合,而这些方法
是需要被增强、被代理的。一般都是按照一定的约定规则来表示的,如正则表达式等。切点是
由一类连接点组成。
    Advice(通知):还是说的通俗点,就是在指定切点上要干些什么。
    Advisor(通知器):其实就是切点和通知的结合 
     
第一种,为最基本的用法,大致步骤为:配置文件中配置pointcut, 在java中用编写实际的aspect 类, 针对对切入点进行相关的业务处理.

   编写目标对象类(CommonEmployee.java),具体代码如下(省略接口类):

Java代码 复制代码
  1. <SPAN style="FONT-SIZE: medium">package com.aop;   
  2.   
  3. public class CommonEmployee implements Employee{   
  4.   
  5.     private String name;   
  6.        
  7.     public String getName() {   
  8.         return name;   
  9.     }   
  10.   
  11.     public void setName(String name) {   
  12.         this.name = name;   
  13.     }   
  14.        
  15.     public void signIn() {   
  16.        System.out.println(name+"已经签到了...........");   
  17.     }   
  18. }</SPAN>  
package com.aop;

public class CommonEmployee implements Employee{

	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void signIn() {
	   System.out.println(name+"已经签到了...........");
	}
}

 

具体通知类Logger.java如下:

Java代码 复制代码
  1. <SPAN style="FONT-SIZE: medium">package com.aop;   
  2.   
  3. import java.util.Date;   
  4. import org.aspectj.lang.ProceedingJoinPoint;   
  5.   
  6. public class Logger{   
  7.   
  8.     //spring中Before通知   
  9.     public void logBefore() {   
  10.         System.out.println("logBefore:现在时间是:....");   
  11.     }   
  12.        
  13.     //spring中After通知   
  14.     public void logAfter() {   
  15.         System.out.println("logAfter:现在时间是:.....");   
  16.     }   
  17.        
  18.     //spring中Around通知   
  19.     public Object logAround(ProceedingJoinPoint joinPoint) {   
  20.         System.out.println("logAround开始:现在时间是:....."); //方法执行前的代理处理   
  21.         Object[] args = joinPoint.getArgs();   
  22.         Object obj = null;   
  23.         try {   
  24.             obj = joinPoint.proceed(args);   
  25.         } catch (Throwable e) {   
  26.             e.printStackTrace();   
  27.         }   
  28.         System.out.println("logAround结束:现在时间是:.....");  //方法执行后的代理处理   
  29.         return obj;   
  30.     }   
  31.        
  32. }</SPAN>  
package com.aop;

import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;

public class Logger{

    //spring中Before通知
	public void logBefore() {
		System.out.println("logBefore:现在时间是:....");
	}
	
	//spring中After通知
	public void logAfter() {
		System.out.println("logAfter:现在时间是:.....");
	}
	
	//spring中Around通知
	public Object logAround(ProceedingJoinPoint joinPoint) {
		System.out.println("logAround开始:现在时间是:....."); //方法执行前的代理处理
		Object[] args = joinPoint.getArgs();
		Object obj = null;
		try {
			obj = joinPoint.proceed(args);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("logAround结束:现在时间是:.....");  //方法执行后的代理处理
		return obj;
	}
	
}

Spring配置

Xml代码 复制代码
  1. <SPAN style="FONT-SIZE: medium"><bean id="employee" class="com.aop.CommonEmployee">  
  2.       <property name="name" value="good"></property>  
  3.     </bean>  
  4.     <bean id="advice" class="com.aop.Logger" />  
  5.    <aop:config >  
  6.      <aop:aspect ref="advice">  
  7.         <aop:pointcut id="pointcut" expression="execution(* com.aop.CommonEmployee.sign*(..))"/>  
  8.         <aop:before method="logBefore" pointcut-ref="pointcut"/>  
  9.         <aop:after method="logAfter" pointcut-ref="pointcut"/>  
  10.         <aop:around method="logAround" pointcut-ref="pointcut"/>  
  11.       </aop:aspect>  
  12.    </aop:config></SPAN>  
<bean id="employee" class="com.aop.CommonEmployee">
      <property name="name" value="good"></property>
    </bean>
    <bean id="advice" class="com.aop.Logger" />
   <aop:config >
     <aop:aspect ref="advice">
        <aop:pointcut id="pointcut" expression="execution(* com.aop.CommonEmployee.sign*(..))"/>
        <aop:before method="logBefore" pointcut-ref="pointcut"/>
        <aop:after method="logAfter" pointcut-ref="pointcut"/>
        <aop:around method="logAround" pointcut-ref="pointcut"/>
      </aop:aspect>
   </aop:config>

 测试类

Java代码 复制代码
  1. <SPAN style="FONT-SIZE: medium">package com;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  5.   
  6. import com.aop.Employee;   
  7.   
  8. public class Test {   
  9.     public static void main(String[] args) throws Exception{   
  10.         ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-aop.xml");   
  11.         Employee e = (Employee)act.getBean("employee");   
  12.         e.signIn();   
  13.            
  14.     }   
  15. }</SPAN>  
package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aop.Employee;

public class Test {
	public static void main(String[] args) throws Exception{
     	ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-aop.xml");
     	Employee e = (Employee)act.getBean("employee");
        e.signIn();
		
	}
}

 测试结果

  logBefore:现在时间是.....

  logAround开始:现在时间是:....

  good已经签到了...........
  logAfter:现在时间是:...

  logAround结束:现在时间是:...... 

 

 

第二种, 采用注解来做aop, 主要是将写在spring 配置文件中的连接点, 写到注解里面

   首先,在spring配置文件中加入如下配置(用来申明spring对@AspectJ的支持):
    <aop:aspectj-autoproxy/>   
如果你使用的是DTD,可以在Spring配置文件中加入如下配置来申明spring对@Aspect的支
持:
  
<bean class="org.springframework.aop.aspectj.annotation.
                       AnnotationAwareAspectJAutoProxyCreator" />

   目标对象类与上边一样

   具体通知类如下

Java代码 复制代码
  1. <SPAN style="FONT-SIZE: medium">package com.aop;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import org.aspectj.lang.ProceedingJoinPoint;   
  6. import org.aspectj.lang.annotation.After;   
  7. import org.aspectj.lang.annotation.Around;   
  8. import org.aspectj.lang.annotation.Aspect;   
  9. import org.aspectj.lang.annotation.Before;   
  10.   
  11. /**  
  12.  * 使用@Aspect 注解的类, Spring 将会把它当作一个特殊的Bean(一个切面),也就是  
  13.  * 不对这个类本身进行动态代理  
  14.  */  
  15. @Aspect     
  16. public class AspectJLogger {   
  17.     /**  
  18.      * 必须为final String类型的,注解里要使用的变量只能是静态常量类型的  
  19.      */  
  20.     public static final String EDP = "execution(* com.aop.CommonEmployee.sign*(..))";   
  21.        
  22.     @Before(EDP)    //spring中Before通知   
  23.     public void logBefore() {   
  24.         System.out.println("logBefore:现在时间是:......");   
  25.     }   
  26.        
  27.     @After(EDP)    //spring中After通知   
  28.     public void logAfter() {   
  29.         System.out.println("logAfter:现在时间是:.....");   
  30.     }   
  31.        
  32.     @Around(EDP)   //spring中Around通知   
  33.     public Object logAround(ProceedingJoinPoint joinPoint) {   
  34.         System.out.println("logAround开始:现在时间是:....."); //方法执行前的代理处理   
  35.         Object[] args = joinPoint.getArgs();   
  36.         Object obj = null;   
  37.         try {   
  38.             obj = joinPoint.proceed(args);   
  39.         } catch (Throwable e) {   
  40.             e.printStackTrace();   
  41.         }   
  42.         System.out.println("logAround结束:现在时间是:.....");  //方法执行后的代理处理   
  43.         return obj;   
  44.     }   
  45. }</SPAN>  
package com.aop;

import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 使用@Aspect 注解的类, Spring 将会把它当作一个特殊的Bean(一个切面),也就是
 * 不对这个类本身进行动态代理
 */
@Aspect  
public class AspectJLogger {
    /**
     * 必须为final String类型的,注解里要使用的变量只能是静态常量类型的
     */
	public static final String EDP = "execution(* com.aop.CommonEmployee.sign*(..))";
	
	@Before(EDP)    //spring中Before通知
	public void logBefore() {
		System.out.println("logBefore:现在时间是:......");
	}
	
	@After(EDP)    //spring中After通知
	public void logAfter() {
		System.out.println("logAfter:现在时间是:.....");
	}
	
	@Around(EDP)   //spring中Around通知
	public Object logAround(ProceedingJoinPoint joinPoint) {
		System.out.println("logAround开始:现在时间是:....."); //方法执行前的代理处理
		Object[] args = joinPoint.getArgs();
		Object obj = null;
		try {
			obj = joinPoint.proceed(args);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("logAround结束:现在时间是:.....");  //方法执行后的代理处理
		return obj;
	}
}

 

 Spring的配置

Xml代码 复制代码
  1. <SPAN style="FONT-SIZE: medium">    <aop:aspectj-autoproxy/>    
  2.     <bean id="aspect" class="com.aop.AspectJLogger" />  
  3.     <bean id="employee" class="com.aop.CommonEmployee">  
  4.       <property name="name" value="good"></property>  
  5.     </bean></SPAN>  
    <aop:aspectj-autoproxy/> 
    <bean id="aspect" class="com.aop.AspectJLogger" />
    <bean id="employee" class="com.aop.CommonEmployee">
      <property name="name" value="good"></property>
    </bean>

 

测试类与上边一样

测试结果

      logBefore:现在时间是:....

      logAround开始:现在时间是:....

      good已经签到了...........
      logAfter:现在时间是:....

      logAround结束:现在时间是:....

注意:
      1.环绕方法通知,环绕方法通知要注意必须给出调用之后的返回值,否
        则被代理的方法会停止调用并返回null,除非你真的打算这么做。 
               
       2.只有环绕通知才可以使用JoinPoint的子类ProceedingJoinPoint,个 
         连接点类型可以调用代理的方法,并获取、改变返回值。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics