利用annotation与AOP对任何方法实现拦截. 附源码下载

摘要: Spring中的注解,也就是annotation 给编程带来了很大的方便, 不用根繁琐的XML去打交道。这一特性主要在于jdk 1.5 开始对注解的支持,并且提供了自定义注解的方法。 利用自定义注解以及spring 和 AOP 的配合,可以对任何类或者任何方法进行拦截。我自己做了一个例子,就是对自己想拦截的方法进行拦截, 可以在方法执行开始,记录日志,在方法执行完成之后,再记录日志等。这仅仅是一个测试,利用这样的特性可以完成更复杂的功能.

Spring中的注解,也就是annotation 给编程带来了很大的方便, 不用根繁琐的XML去打交道。这一特性主要在于jdk 1.5 开始对注解的支持,并且提供了自定义注解的方法。 利用自定义注解以及spring 和 AOP 的配合,可以对任何类或者任何方法进行拦截。我自己做了一个例子,就是对自己想拦截的方法进行拦截, 可以在方法执行开始,记录日志,在方法执行完成之后,再记录日志等。这仅仅是一个测试,利用这样的特性可以完成更复杂的功能.



定义注解

package com.yihaomen.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
}



实现切面
package com.yihaomen.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Aspect
public class LogAspect {

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);


    @Around("@annotation(com.yihaomen.annotation.Log)")
    public Object memoize(ProceedingJoinPoint pjp) throws Throwable {
       
        
        log.info("class name: " + pjp.getSignature().getDeclaringType().getName());
        log.info("method name: " + pjp.getSignature().getName());
        
        for(Object obj : pjp.getArgs()){
        	log.info("parameters: " + obj);
        }
        
       
        Object result = pjp.proceed();
        log.info("end of exec function");
           
        return result;
    }
}


实现一个service, 并实现其方法,在方法上加上我们自定义的注解

package com.yihaomen.service;

public interface ComputeService {

    int compute(int i);
}



package com.yihaomen.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yihaomen.annotation.Log;

@Service(value="myservice")
public class ComputeServiceImpl implements ComputeService {

    private static final Logger log = LoggerFactory.getLogger(ComputeServiceImpl.class);

    @Log
    public int compute(int i) {
       log.info("begin to print inner log " + i);
       return i;
    }
    
}



写测试代码,测试功能是否正常
package com.yihaomen.annotation.test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yihaomen.service.ComputeService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
public class AnnotationTest {

    private static final Logger log = LoggerFactory.getLogger(AnnotationTest.class);

   
    @Autowired   
    private ComputeService computeService;

    @Test
    public void test_compute() {
    	computeService.compute(10);
    }
}


注意还需要的SPRING 的配置文件



    
    
     

    

        
   
  



这里面一定要申明这个切面.

按照这样的方式,在任何你想要加入切面的方法上,加上你自己的自定义注解,就可以了。用起来还是很方便的。代码可以在下面下载,maven 工程的。

annotation spring aop aspect sample code

上一篇: 百度ueditor与spring mvc结合应做的修改.
下一篇: 徒步,仙人掌大峡谷
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号