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

Spring Aop 详解之实例

阅读更多
Spring Aop例子详解


jar要求:

    [spring 3.2 所需jar包;]
    [Aspect所需jar包:aspectjrt.jar,aspectjweaver.jar]


spring 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: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-3.2.xsd  
		   http://www.springframework.org/schema/aop
		   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		   http://www.springframework.org/schema/context
		   http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	
	<!-- AOP的注解形式 -->
	<aop:aspectj-autoproxy />
	
	<bean id="logManage" class="com.xhy.aop.LogManage" />
	
<!--	<bean id="logManageXml" class="com.xhy.aop.LogManageXML" />-->
	
	<bean id="people" class="com.xhy.aop.People" />
	
	<!-- AOP的xml配置 -->
<!--	<aop:config>-->
<!--		<aop:aspect ref="logManageXml">-->
<!--			<aop:pointcut expression="execution(* *.work(..))" id="pointcut"/>-->
<!--			<aop:before method="doBefore" pointcut-ref="pointcut"/>-->
<!--			<aop:after method="doAfter" pointcut-ref="pointcut"/>-->
<!--			<aop:around method="doAround" pointcut-ref="pointcut"/>-->
<!--			<aop:after-returning method="doReturn" pointcut-ref="pointcut"/>-->
<!--			<aop:after-throwing method="doThrow" pointcut-ref="pointcut" throwing="e"/>-->
<!--		</aop:aspect>-->
<!--	</aop:config>-->
	
</beans>

注意:注释的那部分是以xml配置方式运行

目标类:
package com.xhy.aop;
public class People{

	public void work(){
		System.out.println("----人需要工作才能生活!----");
		int x = 5/0;
	}
	
}


注解实现切面类:
@Aspect
public class LogManage {
	
	private static final String EDP = "execution(* *.work())";

	@Pointcut(EDP)
	public void pointMethod(){}
	
	@Before(EDP)
	public void doBefore(){
		System.out.println("方法之前做一些事情~~~");
	}
	
	@After(EDP)
	public void doAfter(){
		System.out.println("方法之后做一些事情~~~");
	}
	
	@AfterReturning(EDP)
	public void doReturn(){
		System.out.println("方法返回后做一些事情~~~");
	}
	
	@Around(EDP)
	public Object doAround(ProceedingJoinPoint joinPoint){
		System.out.println("logAround开始:现在时间是:"+new Date()); 
		System.out.println(joinPoint.getClass().getName());
        Object[] args = joinPoint.getArgs();  
        Object obj = null;  
        try {  
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {  
            e.printStackTrace();  
        }  
        System.out.println("logAround结束:现在时间是:"+new Date()); 
        return obj;  
	}
	
	@AfterThrowing(pointcut="execution(* com.xhy.aop.People.work())",throwing="e")
	public void doThrow(Exception e){
		if(e!=null){  
            System.out.println("执行异常:" + e.getMessage());  
            sendMsg();  
        }  
    }  
	
    private void sendMsg(){  
       System.out.println("发送出错报告(邮件、短信)");  
    }  
	
}

XML实现切面类:
public class LogManageXML {
	

	
	public void doBefore(){
		System.out.println("方法之前做一些事情~~~");
	}
	
	public void doAfter(){
		System.out.println("方法之后做一些事情~~~");
	}
	
	public void doReturn(){
		System.out.println("方法返回后做一些事情~~~");
	}
	
	public Object doAround(ProceedingJoinPoint joinPoint){
		System.out.println("logAround开始:现在时间是:"+new Date()); 
        Object[] args = joinPoint.getArgs();  
        Object obj = null;  
        try {  
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {  
            e.printStackTrace();  
        }  
        System.out.println("logAround结束:现在时间是:"+new Date()); 
        return obj;  
	}
	
	public void doThrow(Exception e){
		 if(e!=null){  
	            System.out.println("执行异常:" + e.getMessage());  
	            sendMsg();  
	        }  
    }  
	
    private void sendMsg(){  
        System.out.println("发送出错报告(邮件、短信)");  
    }  
	
}


测试类:
public class AopTest {

	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("/springConfig/applicationContext_AOP.xml");
		People people = (People)context.getBean("people");
		people.work();
	}
}


运行AopTest查看控制台输出的结果。
我在以注解方式运行的时候发现一个问题,即执行5/0的时候抛出异常。@AfterThrowing标记的方法不执行,xml配置方式完全正常。我也没有找到是什么问题。
网上说是@AfterThrowing 和@Around顺序问题;还有代码中需要直接抛出异常,我都试过,不行。所以这算是个bug吧!如果有哪位高手知道,可以交流下。
0
0
分享到:
评论
1 楼 lvwenwen 2013-09-12  
上传工程吧,小伙伴们找包找的辛苦

相关推荐

    spring aop实例

    **Spring AOP 实例详解** 在Java开发领域,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架的一个重要特性,它允许开发者将关注点从核心...

    spring_aop_拦截实例

    在"Spring事务详解.pdf"中,会深入讲解Spring的事务管理,这是AOP的一个典型应用。Spring提供了声明式事务管理,允许开发者通过注解或XML配置来控制事务的边界,而无需手动管理事务的开始、提交和回滚。例如,`@...

    springAOP详解

    ### Spring AOP 详解 #### 5.1 AOP 基本思想 **5.1.1 认识 AOP** AOP (Aspect Oriented Programming) 面向切面编程,是一种软件开发思想,它允许开发者将那些分散在整个应用各处的“切面”(如日志记录、安全控制...

    Spring之AOP配置文件详解

    ### Spring之AOP配置文件详解 #### 一、前言 在Java开发中,Spring框架因其强大的功能和灵活的配置而被广泛应用于企业级应用的开发。其中,面向切面编程(Aspect Oriented Programming,简称AOP)是Spring框架的...

    spring aop小实例

    在IT行业中,Spring框架是Java企业级应用开发的首选框架,而Spring AOP(面向切面编程)则是其核心特性之一。AOP提供了一种在不修改代码的情况下,对现有程序进行扩展和增强的能力,主要应用于日志记录、事务管理、...

    spring aop 注解例子

    **Spring AOP 注解例子详解** 在 Spring 框架中,面向切面编程(Aspect Oriented Programming,AOP)是一种强大的设计模式,它允许我们分离关注点,将业务逻辑与系统服务(如日志、事务管理等)解耦。在 Spring AOP...

    基于java的企业级应用开发:Spring AOP简介.ppt

    **AOP术语详解** 1. **Aspect**:切面是AOP的核心概念,它封装了特定的横切关注点,例如事务管理和日志记录。一个切面可以包含多个通知(Advice)和切点(Pointcut)。 2. **Joinpoint**:连接点是指程序执行过程...

    Spring AOP 入门作者:廖雪峰

    ### Spring AOP 入门详解 #### 一、Spring AOP 概述 Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个关键特性,它为开发者提供了在运行时动态添加代码(即横切关注点或切面)到已有...

    java Spring AOP详解及简单实例

    Java Spring AOP详解及简单实例 一、什么是AOP AOP(Aspect Oriented Programming)是面向切面编程,它不同于OOP(Object Oriented Programming)面向对象编程。AOP是将程序的运行看成一个流程切面,其中可以在切...

    Spring Aop详解

    Spring 中的Aop面向切面编程详解,具体实例。

    仿springAOP框架

    **仿Spring AOP框架详解** 在Java开发领域,Spring框架以其强大的功能和灵活性深受开发者喜爱,其中AOP(面向切面编程)是其核心特性之一。AOP允许开发者将关注点从主业务逻辑中分离出来,如日志记录、事务管理等,...

    spring2-aop入门实例教程

    ### Spring2-AOP入门实例教程知识点详解 #### 一、Spring框架概述 - **轻量级J2EE开发框架**:Spring是一个轻量级的Java应用框架,它为开发复杂的企业级应用提供了一种简化的方法。 - **发展历程**:自2002年发布...

    SpringAOP的实现机制(底层原理)、应用场景等详解,模拟过程的实例

    这两种代理方式在Spring AOP中起到关键作用,用于实现横切关注点的切面编程。通过学习它们的原理和实际应用,您将能够更好地理解和利用Spring AOP来提高您的应用程序的可维护性和可扩展性。 内容亮点: JDK动态...

    Spring AOP执行先后顺序实例详解

    Spring AOP执行先后顺序实例详解 Spring AOP(Aspect-Oriented Programming)是一种面向方面的编程思想,它可以将散乱、分散的关注点(Concerns)模块化,通过方面(Aspect)来封装这些关注点,从而实现松耦合、...

    SpringAOP+AspectJ

    **Spring AOP与AspectJ详解** 在现代软件开发中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们分离关注点,将横切关注点(如日志、事务管理、权限控制等)与核心业务...

    最简单的SpringAOP入门案例

    **Spring AOP 入门案例详解** 在Java开发中,Spring框架因其强大的功能和灵活性而备受推崇,其中Spring AOP(Aspect Oriented Programming,面向切面编程)是它的一个重要特性。AOP允许开发者将关注点从核心业务...

Global site tag (gtag.js) - Google Analytics