IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    asuncool@gmail.com(yihaomen)发表于 2015-05-12 21:59:22
    love 0
    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 的配置文件
    程序代码 程序代码


    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-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">








    class="com.yihaomen.annotation.LogAspect"/>





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

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

    下载文件 annotation spring aop aspect sample code


沪ICP备19023445号-2号
友情链接