`
youyu4
  • 浏览: 435914 次
社区版块
存档分类
最新评论

Spring AOP 之AspectJ注解配置

 
阅读更多

Spring AOP 之AspectJ注解配置

 

使用JDK代理

 

业务类

 

package com.tgb.aop;  
  
public interface UserManager {  
  
    public String findUserById(int userId);  
}  
  
  
package com.tgb.aop;  
  
public class UserManagerImpl implements UserManager {  
  
    public String findUserById(int userId) {  
        System.out.println("---------UserManagerImpl.findUserById()--------");  
        if (userId <= 0) {  
            throw new IllegalArgumentException("该用户不存在!");   
        }  
        return "张三";  
    }  
}  

 

 

Advice类

 

package com.tgb.aop;  
  
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.AfterThrowing;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.DeclareParents;  
import org.aspectj.lang.annotation.Pointcut;  
  
/** 
 * 测试after,before,around,throwing,returning Advice. 
 * @author Admin 
 * 
 */  
@Aspect  
public class AspceJAdvice {  
  
    /** 
     * Pointcut 
     * 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数 
     * 该方法就是一个标识,不进行调用 
     */  
    @Pointcut("execution(* find*(..))")  
    private void aspectjMethod(){};  
      
    /**  
     * Before 
     * 在核心业务执行前执行,不能阻止核心业务的调用。 
     * @param joinPoint  
     */    
    @Before("aspectjMethod()")    
    public void beforeAdvice(JoinPoint joinPoint) {    
        System.out.println("-----beforeAdvice().invoke-----");  
        System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of beforeAdvice()------");  
    }  
        
    /**  
     * After  
     * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice 
     * @param joinPoint 
     */  
    @After(value = "aspectjMethod()")    
    public void afterAdvice(JoinPoint joinPoint) {    
        System.out.println("-----afterAdvice().invoke-----");  
        System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of afterAdvice()------");  
    }    
  
    /**  
     * Around  
     * 手动控制调用核心业务逻辑,以及调用前和调用后的处理, 
     *  
     * 注意:当核心业务抛异常后,立即退出,转向AfterAdvice 
     * 执行完AfterAdvice,再转到ThrowingAdvice 
     * @param pjp 
     * @return 
     * @throws Throwable 
     */   
    @Around(value = "aspectjMethod()")    
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {    
        System.out.println("-----aroundAdvice().invoke-----");  
        System.out.println(" 此处可以做类似于Before Advice的事情");  
          
        //调用核心逻辑  
        Object retVal = pjp.proceed();  
        System.out.println(" 此处可以做类似于After Advice的事情");  
        System.out.println("-----End of aroundAdvice()------");  
        return retVal;  
    }    
      
    /**  
     * AfterReturning  
     * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice 
     * @param joinPoint 
     */   
    @AfterReturning(value = "aspectjMethod()", returning = "retVal")    
    public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {    
        System.out.println("-----afterReturningAdvice().invoke-----");  
        System.out.println("Return Value: " + retVal);   
        System.out.println(" 此处可以对返回值做进一步处理");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of afterReturningAdvice()------");  
    }  
      
    /** 
     * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息 
     *  
     * 注意:执行顺序在Around Advice之后 
     * @param joinPoint 
     * @param ex 
     */  
    @AfterThrowing(value = "aspectjMethod()", throwing = "ex")    
    public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {    
        System.out.println("-----afterThrowingAdvice().invoke-----");  
        System.out.println(" 错误信息:"+ex.getMessage());  
        System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of afterThrowingAdvice()------");    
    }    
}  

 

 

applicationContext.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"  
         xmlns:tx="http://www.springframework.org/schema/tx"  
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
             
    <!-- 启用AspectJ对Annotation的支持 -->         
    <aop:aspectj-autoproxy/>             
      
    <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>  
    <bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>  
      
</beans>  

 只要将Advice类注入到bean,Spring会根据切入点,找到相应的连接点切入

 

测试类

package com.tgb.aop;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Client {  
  
    public static void main(String[] args) {  
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
        UserManager userManager = (UserManager)factory.getBean("userManager");  
          
        //可以查找张三  
        userManager.findUserById(1);  
          
        System.out.println("=====我==是==分==割==线=====");  
  
        try {  
            // 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获  
            userManager.findUserById(0);  
        } catch (IllegalArgumentException e) {  
        }  
    }  
}  

 

测试结果



 

 

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

相关推荐

    Spring Aop之AspectJ注解配置实现日志管理的方法

    Spring Aop之AspectJ注解配置实现日志管理的方法 Spring Aop是基于AspectJ实现的面向切面编程(AOP),它提供了一个灵活的方式来实现日志管理。通过使用AspectJ注解,可以轻松地实现日志记录、性能监控、安全检查...

    Spring AOP之基于AspectJ注解总结与案例

    本篇内容将对Spring AOP中基于AspectJ注解的使用进行总结,并通过实际案例进行解析。 首先,让我们理解AspectJ注解在Spring AOP中的核心概念: 1. **@Aspect**: 这个注解用于定义一个类为切面,这个类将包含切点和...

    Spring AOP + AspectJ annotation example

    本篇将深入探讨如何结合Spring AOP和AspectJ注解进行实践。 首先,我们需要理解AOP的基本概念。面向切面编程是一种编程范式,它允许程序员定义“切面”,这些切面封装了跨越多个对象的行为或关注点。在Spring中,切...

    Spring AOP + AspectJ in XML 配置示例

    这篇博客“Spring AOP + AspectJ in XML配置示例”旨在指导开发者如何在XML配置中实现Spring AOP和AspectJ的结合。 首先,我们需要理解AOP的基本概念。AOP通过将关注点(如日志、事务管理)与业务逻辑分离,提高了...

    Spring AOP @AspectJ 入门实例

    在IT行业中,Spring框架是Java企业级应用开发的首选,而Spring AOP(面向切面编程)则是其核心特性之一,用于实现横切关注点的模块化,如日志、事务管理等。本实例将带你深入理解并实践Spring AOP与@AspectJ的结合...

    SpringAOP的注解配置

    在Spring AOP中,我们可以通过注解配置来实现切面编程,从而简化代码并提高可维护性。 首先,我们需要了解Spring AOP中的核心概念: 1. **切面(Aspect)**:切面是关注点的模块化,它包含了横切关注点(如日志)和...

    @AspectJ配置Spring AOP,demo

    `springAOP2`可能是一个包含具体示例代码的目录。`基于@AspectJ配置Spring AOP之一 - 飞扬部落编程仓库-专注编程,网站,专业技术.htm`和其关联的`_files`目录可能包含了一个详细的教程或演示如何配置和运行@AspectJ的...

    Spring AOP 概念理解及@AspectJ支持

    **Spring AOP 概念理解** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要组成部分,它允许我们通过...理解和熟练运用Spring AOP及其@AspectJ注解是每个Spring开发者必备的技能之一。

    征服Spring AOP—— @AspectJ

    在IT行业中,Spring框架是Java企业级应用开发的首选,而Spring AOP(面向切面编程)则是其核心特性之一,用于实现横切关注点的模块化,如日志、事务管理等。@AspectJ是Spring AOP的一种注解驱动方式,它极大地简化了...

    jar包---Spring Aop AspectJ新增包.rar

    2. 配置Spring,声明启用AspectJ自动代理,可以通过`&lt;aop:aspectj-autoproxy&gt;`标签实现。 3. 定义切面(Aspect),切面是AOP的核心,包含切点(Pointcut)和通知(Advice)。切点是关注点的具体定位,通知是切点发生...

    spring-aop-aspectj(Schema)-case

    在Spring AOP中,AspectJ有两种集成方式:一种是基于注解(@Aspect),另一种是基于XML Schema配置。本案例可能侧重于后者,即XML配置方式。这种方式通常在需要更精细控制或与老项目集成时使用。 描述中提到的"NULL...

    spring-aop-aspectj-case

    - **&lt;aop:aspectj-autoproxy/&gt;**:在Spring配置文件中启用AspectJ自动代理。 4. **实际应用示例**: - 日志记录:通过切面记录方法的调用时间、参数等信息。 - 事务管理:利用AOP进行数据库事务的开启、提交、...

    aspectj的jar spring使用aop需要的jar

    8. **类型匹配(Type Matching)**:AspectJ的强项之一是类型匹配能力,它允许你基于类或接口来定义切入点,而不仅仅是方法签名,这比Spring AOP的基于方法签名的切入点更灵活。 9. **注解驱动(Annotation-Based)...

    SpringAOP中的注解配置详解

    SpringAOP中的注解配置详解 ...SpringAOP中的注解配置详解是实现AOP功能的重要技术之一,它提供了一种灵活、可扩展的方式来拦截和扩展业务逻辑。通过使用注解和XML配置,开发者可以轻松地实现对业务逻辑的拦截和扩展。

    spring注解aop配置详解

    Spring AOP,即Aspect-Oriented Programming(面向切面编程),是Spring框架的重要特性,它为应用程序提供了声明式的企业级服务,如...在实际开发中,熟练掌握Spring AOP的注解配置无疑会极大地提升我们的工作效率。

    spring aop注解版

    在本主题中,我们将深入探讨Spring AOP的注解版,它是基于Java注解的实现,简化了配置并提高了代码的可读性。 首先,让我们理解AOP的基本概念。AOP是一种编程范式,允许程序员定义“切面”,这些切面封装了跨越多个...

    springboot spring aop 拦截器注解方式实现脱敏

    启动类通常会包含`@SpringBootApplication`注解,该注解包含了`@EnableAutoConfiguration`,`@ComponentScan`和`@SpringBootConfiguration`,它们一起告诉Spring Boot自动配置应用,并扫描组件。启动类可能会看起来...

    Spring AOP 16道面试题及答案.docx

    Spring支持两种AOP的实现方式:Spring AspectJ注解风格和Spring XML配置风格。使用AspectJ注解风格是最常见的,它允许开发者直接在方法上使用注解来定义切面。 Spring AOP中有五种不同类型的的通知(Advice): 1....

    hualinux spring 3.15:Spring AOP.pdf

    在Spring框架中,从2.0版本开始支持两种方式来使用AOP:基于AspectJ注解的AOP以及基于XML配置的AOP。 4. 在Spring中启用AspectJ注解支持 要在Spring应用中使用AspectJ注解,需要在classpath下包含三个JAR文件:aop...

Global site tag (gtag.js) - Google Analytics