`

spring4 aop 使用

阅读更多

1.下载 jar 

 

 //core spring  

    compile "org.springframework:spring-context:$springVersion"

    compile "org.springframework:spring-core:$springVersion"

    compile "org.springframework:spring-webmvc:$springVersion"

    compile "org.springframework:spring-aop:$springVersion"

   

    compile "org.springframework:spring-expression:$springVersion"

    compile "org.springframework:spring-beans:$springVersion"

    compile "org.springframework:spring-aspects:$springVersion"

compile group: 'org.aopalliance', name: 'com.springsource.org.aopalliance', version: '1.0.0'

compile group: 'org.aspectj', name: 'com.springsource.org.aspectj.weaver', version: '1.6.8.RELEASE'

    

 

1.定义接口

package com.boce.aop;

 

public interface AopTest {

 

public int add(int i, int j);

 

    public int sub(int i, int j);

 

    public int mul(int i, int j);

 

    public int div(int i, int j);

 

}

 

2.接口实现:

package com.boce.aop;

 

import org.springframework.stereotype.Component;

 

@Component

public class AopTestImp  implements AopTest{

 

public int add(int i, int j) {

int result = i + j;

return result;

}

 

 

public int sub(int i, int j) {

int result = i - j;

return result;

}

 

 

public int mul(int i, int j) {

int result = i * j;

return result;

}

 

 

public int div(int i, int j) {

int result = i / j;

return result;

}

}

 

 

3.aop 拦截:

 

package com.boce.aop;

 

import java.util.Arrays;

import java.util.List;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

 

@Order(1)

@Aspect  

@Component

public class AopLogAspect {

/**

     * 声明切入点表达式,一般在该方法中不再添加其他代码。

     * 使用@Pointcut来声明切入点表达式。

     * 后面的通知直接使用方法名来引用当前的切入点表达式。

     */

    @Pointcut("execution(public int com.boce.aop.AopTestImp.*(..))")

    public void declareJoinPointExpression() {}

 

    /**

    *前置通知,在目标方法开始之前执行。

    *@Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.add(int, int))")这样写可以指定特定的方法。

     * @param joinpoint

     */

    @Before("declareJoinPointExpression()")

    //这里使用切入点表达式即可。后面的可以都改成切入点表达式。如果这个切入点表达式在别的包中,在前面加上包名和类名即可。

    public void beforeMethod(JoinPoint joinpoint) {

        String methodName = joinpoint.getSignature().getName();

        List<Object>args = Arrays.asList(joinpoint.getArgs());

        System.out.println("前置通知:The method "+ methodName +" begins with " + args);

    }

 

    /**

    *后置通知,在目标方法执行之后开始执行,无论目标方法是否抛出异常。

    *在后置通知中不能访问目标方法执行的结果。

     * @param joinpoint

     */

    @After("execution(public int com.boce.aop.AopTestImp.*(int, int))")

    public void afterMethod(JoinPoint joinpoint) {

        String methodName = joinpoint.getSignature().getName();

        //List<Object>args = Arrays.asList(joinpoint.getArgs());  后置通知方法中可以获取到参数

        System.out.println("后置通知:The method "+ methodName +" ends ");

    }

     

    /**

    *返回通知,在方法正常结束之后执行。

    *可以访问到方法的返回值。

     * @param joinpoint

     * @param result 目标方法的返回值

     */

    @AfterReturning(value="execution(public int com.boce.aop.AopTestImp.*(..))", returning="result")

    public void afterReturnning(JoinPoint joinpoint, Object result) {

        String methodName = joinpoint.getSignature().getName();

        System.out.println("返回通知:The method "+ methodName +" ends with " + result);

    }

     

    /**

    *异常通知。目标方法出现异常的时候执行,可以访问到异常对象,可以指定在出现特定异常时才执行。

    *假如把参数写成NullPointerException则只在出现空指针异常的时候执行。

     * @param joinpoint

     * @param e

     */

    @AfterThrowing(value="execution(public int com.boce.aop.AopTestImp.*(..))", throwing="e")

    public void afterThrowing(JoinPoint joinpoint, Exception e) {

        String methodName = joinpoint.getSignature().getName();

        System.out.println("异常通知:The method "+ methodName +" occurs exception " + e);

    }

     

    /**

     * 环绕通知类似于动态代理的全过程,ProceedingJoinPoint类型的参数可以决定是否执行目标方法。

     * @param point 环绕通知需要携带ProceedingJoinPoint类型的参数。

     * @return 目标方法的返回值。必须有返回值。

     */

     /*

    @Around("execution(public int com.boce.aop.AopTestImp.*(..))")

    public Object aroundMethod(ProceedingJoinPoint point) {

        Object result = null;

        String methodName = point.getSignature().getName();

        try {

            //前置通知

            System.out.println("The method "+ methodName +" begins with " + Arrays.asList(point.getArgs()));

            //执行目标方法

            result = point.proceed();

            //翻译通知

            System.out.println("The method "+ methodName +" ends with " + result);

        } catch (Throwable e) {

            //异常通知

            System.out.println("The method "+ methodName +" occurs exception " + e);

            throw new RuntimeException(e);

        }

        //后置通知

        System.out.println("The method "+ methodName +" ends");

        return result;

    }

    */

 

}

 

3.配置文件:

<?xml version="1.0" encoding="UTF-8"?>

 

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

 

<context:component-scan base-package="com.boce.aop">

</context:component-scan>

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

</beans>

 

4.测试类

package com.boce.aop;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.boce.aop.AopTest;

 

//@RunWith(SpringJUnit4ClassRunner.class)

//@ContextConfiguration(locations = { "classpath:aopContext-service.xml" })

public class AopTestMain {

 

public static void main(String[] args) {

 

// 创建spring IOC容器

ApplicationContext applicationContext = new

ClassPathXmlApplicationContext("aopContext-service.xml");

// 从IOC容器中获取bean实例

AopTest aopTest = applicationContext.getBean(AopTest.class);

int result = aopTest.add(4, 6);

 

System.out.println(result + "");

result = aopTest.sub(4, 6);

System.out.println(result);

System.out.println(result);

result = aopTest.mul(4, 6);

System.out.println(result);

System.out.println(result);

result = aopTest.div(4, 0);

System.out.println(result);

 

}

 

}

 

注意:当对Controller 层进行aop 拦截时,配置文件一定要配置在 spring-mvc 的文件中,否则AOP无效

执行结果:



 

 

 

 

 

  • 大小: 65.5 KB
分享到:
评论

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring-aop-jar

    在IT领域,Spring框架是一个广泛使用的Java应用框架,它提供了许多功能,包括依赖注入、面向切面编程(AOP)等。"spring-aop-jar"这个主题涉及到Spring框架中的核心组件之一——Spring AOP。这里我们将深入探讨...

    Spring 2.5 AOP 例子

    4. `&lt;aop:before&gt;`、`&lt;aop:after&gt;`、`&lt;aop:around&gt;`、`&lt;aop:after-returning&gt;`和`&lt;aop:after-throwing&gt;`:分别用于定义在目标方法执行前、后、环绕、成功返回和抛出异常时执行的通知。 四、AOP术语 - 切面(Aspect...

    Spring使用AOP的三个jar包

    要使用Spring的AOP功能,我们需要引入特定的库,这正是标题中提到的"Spring使用AOP的三个jar包"。 首先,我们来看`aspectjrt.jar`。这个库是AspectJ运行时环境的一部分,包含了运行时织入(runtime weaving)所需的...

    spring-boot aop

    4. **使用代理**:Spring会根据需要自动创建代理对象来处理通知。默认情况下,Spring使用基于Java的代理,但对于需要在静态方法或非Spring管理对象上应用AOP的情况,可能需要使用CGLIB或AspectJ字节码代理。 5. **...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring之AOP(动态代理)

    在Spring Boot项目中,配置和使用AOP相对简单。首先,我们需要引入Spring AOP和AspectJ的依赖,这通常在pom.xml或build.gradle文件中完成。然后,我们可以定义一个切面(Aspect),它包含通知(Advice)——即在特定...

    spring,spring-aop-5.3.22.jar+aop+IDEA本地包

    spring-aop-5.3.22.jar Spring AOP provides an Alliance-compliant aspect-oriented programming implementation allowing you to define method interceptors and pointcuts to cleanly decouple code that ...

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    Spring进行AOP操作需要的4个jar包

    Spring进行aop操作需要的4个jar包,包括aopalliance-1.0.jar、aspectjweaver-1.8.9.jar、spring-aop-4.3.0.RELEASE.jar、spring-aspects-4.3.0.RELEASE.jar。

    spring-aop-6.0.2.jar

    spring-aop-6.0.2.jar

    spring4 AOP 面向切面编程@Aspect

    下面我们将详细探讨Spring 4中的AOP和`@Aspect`的使用。 首先,了解AOP的基本概念。AOP通过“切面”来封装横切关注点,切面是跨越多个对象的行为或责任的模块化。在Spring中,一个切面可以由通知(advisors)、切入...

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    Spring Aop四个依赖的Jar包

    要使用Spring AOP,通常需要引入以下几个核心的Jar包: 1. **aspectj-1.7.3.jar**:这是AspectJ库的核心部分,提供了AOP语言支持,包括AspectJ编译器和运行时库。AspectJ是Java平台上的一个开源项目,它扩展了Java...

    spring-aop-3.2.0.RELEASE.jar

    spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包

    Spring_aop源码

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的...通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中

    spring-aop.jar

    本文将围绕spring-aop.jar这个核心组件,详细探讨Spring AOP的原理、使用方式以及它在实际开发中的应用。 一、AOP概述 AOP是一种编程范式,旨在减少代码冗余,提高模块间的解耦度,通过将关注点分离到不同的切面中...

Global site tag (gtag.js) - Google Analytics