`
chaoren3166gg
  • 浏览: 20165 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring aop中实现变更方法参数以及annotation

阅读更多
  1. 实现功能:
  • 利用spring aop annotation实现切片操作,并改变方法参数的值
  • 判断方法或方法的参数上是否有特定的annotation,如果有,则按照其它逻辑进行处理
  1. aop切片操作
@Component
@Aspect
public class AopAnnotation {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(public * com.*.*.*(..))")
    public void pointCut(){

    }

    @Before(value = "pointCut()")
    public void before(){
        logger.info("annotation,   before################################");
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint){
        logger.info("annotation,   around################################");
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.start();
        try {
            joinPoint.proceed();
        } catch (Throwable e) {

        }
        stopwatch.stop();
        logger.info("annotation, ###################cost time: " + joinPoint.getThis().getClass().getName() +
                "\t" + stopwatch.elapsedMillis() + "(ms)");
    }

    @AfterThrowing(pointcut = "pointCut()")
    public void aa(){
          logger.info("error");
    }

    
    public Verifier verifier;
}

 

  1. 声明annotation
  • @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface Param {
      String value();
    }
     
  1. 在joinpoint中进行处理,判断参数是否有annotation
  • @Around("sqlEscapePoint()")
        public Object sqlEscapeAdvice(ProceedingJoinPoint joinPoint) {
            logger.info("aroundSqlEscape,   around################################");
            Object[] objects = joinPoint.getArgs();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Annotation[][] annotations = method.getParameterAnnotations();
            for (int i = 0; i < objects.length; i++) {
                Object tmpObject = objects[i];
                boolean notEscape = false;
                if (annotations.length > 0) {
                    for (Annotation annotation : annotations[i]) {
                        if (annotation.annotationType() == NotEscapeSql.class) {
                            notEscape = true;
                        }
                    }
                }
                if (notEscape) {
                    continue;
                }
                objects[i] = EscapeUtil.escape(tmpObject, false, false, true);
            }
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.start();
            Object result = null;
            try {
                result = joinPoint.proceed(objects);
            } catch (Throwable e) {
                logger.error("sql escape", e);
            }
            stopwatch.stop();
            logger.info("mapper cost time: " + joinPoint.getThis().getClass().getName() + ","
                    + method.getName() + "\t" + stopwatch.elapsedMillis() + "(ms)");
            return result;
        }
     
1
1
分享到:
评论

相关推荐

    基于AOP+Annotation的AutoLoadCache设计源码

    本文所介绍的“AutoLoadCache”是一个利用面向切面编程(AOP)和注解(Annotation)技术实现的自动加载缓存机制,旨在简化开发者的代码,同时保证缓存的灵活管理和高效性能。 项目背景: 在复杂的业务逻辑中,...

    quartz在Spring中的配置

    ### quartz在Spring中的配置 #### 一、简介与基本配置 Quartz 是一款开源的任务调度框架,被广泛应用...此外,还可以进一步利用 Spring 的依赖注入和 AOP 特性来增强任务调度的功能,例如实现日志记录、异常处理等。

    SSH框架面试题.pdf

    Spring通过AOP代理实现了面向切面编程的功能,允许开发者通过配置或注解的方式将这些横切关注点织入到正常的业务逻辑中,而不必在业务代码中显式实现。 24. **解释Spring MVC的控制器(Controller)** Spring MVC...

    开发项目过程中最全的配置文件模板

    本文将深入探讨"开发项目过程中最全的配置文件模板",包括Spring配置文件的基础知识、重要元素以及如何有效地管理和使用这些模板。 首先,Spring配置文件通常以XML格式存在,命名为`applicationContext.xml`,它是...

    ssm例子一直报错userDao无法注入

    3. **AOP代理问题**:如果`UserDao`使用了Spring AOP代理,那么在尝试注入时可能需要使用`@Autowired`注解的`@Qualifier`属性来指定确切的bean,或者使用`@Resource`注解来按名称注入。 4. **Spring MVC配置**:在...

    Spring Boot入门(web+freemarker)

    import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello Spring...

    JAVA公共资源模块的设计与开发(源代码+l文).zip

    2. **Spring Framework**:Spring提供了强大的依赖注入和AOP(面向切面编程)功能,便于模块化开发和解耦。 3. **设计模式**:如工厂模式用于创建对象,单例模式用于保证类只有一个实例,观察者模式用于事件驱动等...

    Java:java项目实战练习

    2. **Spring框架**:这是一个广泛使用的Java企业级应用框架,提供了依赖注入、AOP(面向切面编程)、事务管理等功能。 3. **数据库连接**:可能涉及到JDBC(Java Database Connectivity)用于与MySQL、Oracle等...

    我的项目

    11. **Spring框架**:考虑到项目可能使用了Java生态系统中的Spring框架,它是企业级应用开发的主流选择,提供了依赖注入、AOP(面向切面编程)、数据访问、Web服务等功能。 12. **Maven或Gradle**:MyProject-main...

    RealProject

    15. **Spring框架**:如果项目涉及到服务端开发,Spring可能是主要的框架,提供依赖注入、AOP、数据访问等能力。 "RealProject-master"的命名方式通常表示这是一个Git仓库的主分支。Git是一种分布式版本控制系统,...

Global site tag (gtag.js) - Google Analytics