美文网首页
web填坑-AOP

web填坑-AOP

作者: 在南方的北方人_Elijah | 来源:发表于2017-02-22 00:52 被阅读34次

AOP面向切面编程

AOP AOP 横切逻辑 AOP术语

利用Spring注解方式实现AOP功能

注解实现AOP功能

student.java

package com.jike.spring.chapter09.aop.aspect;

public class Student {
            public String print(String name){  
                    System.out.println("print() method:" + name);  
                    return "hello";  
                }  

}

StuInterceptor.java

package com.jike.spring.chapter09.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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 StuInterceptor {

    /**
     * 打印方法AOP
     */
    @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.print(..))")
    // @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.*(..))")
    public void printMethod() {
    }

    @Before("printMethod()")
    public void printBeforeAdvice() {
        System.out.println("printBeforeAdvice()!");
    }

    @AfterReturning(pointcut = "printMethod()", returning = "flag")
    public void printAfterAdvice(String flag) {
        System.out.println("printAfterAdvice()! " + flag);
    }

    @After("printMethod()")
    public void finallyAdvice() {
        System.out.println("finallyAdvice()!");
    }

    @Around("printMethod() && args(name)")
    public Object printAroundAdvice(ProceedingJoinPoint pjp, String name)
            throws Throwable {
        Object result = null;
        if (name.equals("whc"))
            pjp.proceed();
        else
            System.out.println("print()方法以及被拦截...");
        return result;
    }
}

  • @Pointcut指出了切入点就是print方法,限免的注释,使用通配符指出切入点为所有的Student类中的方法,第一个星号*,表示返回值可以为任意类型,第二个为方法通配符,括号中的两个点,表示的是任意参数

  • @Before定义了一个前置通知,即在printMethod前运行。

  • @AferReturning定义了一个后置通知,后面的returning表示获取方法的返回值,并放入flag变量中,和后置方法中的参数对应。

  • @After最终通知。

  • @Around环绕通知,方法中又一个ProceedingJoinPoint pjp,在下面的判断语句中,表示如果符合条件,则pjp.proceed().表示程序继续执行,若不符合,则不运行pjp.proceed(),程序不能继续执行。

conf-aspect.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <aop:aspectj-autoproxy />
    <bean id="stuInterceptor" class="com.jike.spring.chapter09.aop.aspect.StuInterceptor"></bean>
    <bean id="stu" class="com.jike.spring.chapter09.aop.aspect.Student"></bean>
</beans>    

这里的<aop:aspectj-autoproxy />配置了aop切面代理的自动加载。

main.java入口类

package com.jike.spring.chapter09.aop.aspect;

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

public class Main {
     public static void main(String[] args) {  
               ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/conf-aspect.xml");  
                 Student stu = (Student)ctx.getBean("stu");  
                    stu.print("whc");  
                }  

}

打印结果

相关文章

  • web填坑-AOP

    AOP面向切面编程 利用Spring注解方式实现AOP功能 student.java StuInterceptor...

  • android flutter 文章汇总

    flutter环境搭建flutter填坑flutter 技术专辑flutter 开发web应用flutter 开发...

  • Web填坑记录册

    Webpack 中 less @import 使用 alias 相对路径的方法 @import "./../../...

  • Flutter for web 最新填坑

    最新更新: 无意中翻看源码,发现有趣的代码: 代码文件路径:packages/flutter/lib/src/fo...

  • Keras 的 Web 填坑记

    0x00 前言 特别声明:当你在看这篇文章时,你需要对 keras 的基本用法已经了解和使用,并且具有简单的Pyt...

  • web填坑-Struts2

    Struts2实例 src下创建com.Struts2Test.HelloStrutsLoginAction.ja...

  • web填坑日记-Spring(1)

    Spring IOC(控制反转) IOC理论背景 IOC(Inversion of Control,控制反转 )...

  • web填坑-Spring入门(2)

    Spring入门例子 创建一个基于SpringIOC的小程序的步骤如下: 建立Spring工程 编写Java文件 ...

  • 再挖个坑,明天填,push的

    再挖个坑,明天填,push的 再挖个坑,明天填,push的 再挖个坑,明天填,push的 再挖个坑,明天填,pus...

  • SpringMVC入门

    参考资料: SpringMVC入门学习SpringMVC工作原理web开发功能权限控制 填坑 1 jdk1.8和t...

网友评论

      本文标题:web填坑-AOP

      本文链接:https://www.haomeiwen.com/subject/ndkpwttx.html