`

Spring AOP annotation 简单实例

 
阅读更多

最后输出(可以看出各种通知的时间):

 

我是前置通知。
我是环绕--进。
aa 已成功保存
我是后置通知。
我是最终通知。
我是环绕--出。

 

app.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.mhm.spring"/>
	
</beans>

 

MyAOP.Java

package com.mhm.spring.mng.impl;

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.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAOP {

	//声明切入点
        //注意,impl下面的类必须实现了mng中的接口,不然不能实现代理,但如果还是要使用代理
        //那么,在项目中加入cglib,它会根据二进制来实现代理
	@Pointcut("execution(* com.mhm.spring.mng.impl..*.*(..))")
	public void anyMethod(){};
	
	//前置通知
	@Before("anyMethod()")
	public void dobefore() {
		System.out.println("我是前置通知。");
	}
	
	//后置通知
	@AfterReturning("anyMethod()")
	public void doafterReturning() {
		System.out.println("我是后置通知。");
	}
	
	//最终置通知
	@After("anyMethod()")
	public void doafter() {
		System.out.println("我是最终通知。");
	}
	
	//异常通知
	@AfterThrowing("anyMethod()")
	public void doexception() {
		System.out.println("我是异常通知。");
	}
	
	//环绕通知
	@Around("anyMethod()")
	public Object doprocess(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("我是环绕--进。");
		Object o = pjp.proceed();
		System.out.println("我是环绕--出。");
		return o;
	}
	
}

 

PersonMng.java

package com.mhm.spring.mng;

public interface PersonMng {
	public void save(String name);
	public String get(int id);
	public void delete(String id);
}

 

PersonMngImpl:

package com.mhm.spring.mng.impl;

import org.springframework.stereotype.Service;

@Service
public class PersonMngImpl implements PersonMng {

	@Override
	public void delete(String id) {
		System.out.println(id + " 已成功删除。");
	}

	@Override
	public String get(int id) {
		return "返回ID为 " + id + " 的人";
	}

	@Override
	public void save(String name) {
		//throw new RuntimeException("我是运行时异常。");
		System.out.println(name + " 已成功保存");
	}

}

 

测试:package com.mhm.spring.mng.impl;

import org.junit.AfterClass;

public class PersonMngImplTest {

	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
	}

	@Test
	public void save() {
		try{
			ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
			PersonMng personMng = (PersonMng)context.getBean("personMngImpl");
			personMng.save("aa");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

}

 

//下面是稍微复杂点的MyAOP

package com.mhm.spring.mng.impl;

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.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAOP {

	//声明切入点
	@Pointcut("execution(* com.mhm.spring.mng.impl..*.*(..))")
	public void anyMethod(){};
	
	//前置通知
	//拦截参数为String类型的方法
	@Before("anyMethod() && args(name)")
	public void dobefore(String name) {
		System.out.println("我是前置通知。" + name);
	}
	
	//后置通知
	//拦截 返回类型为String 的方法
	@AfterReturning(pointcut="anyMethod()", returning="result")
	public void doafterReturning(String result) {
		System.out.println("result: " + result);
		System.out.println("我是后置通知。" );
	}
	
	//最终置通知
	@After("anyMethod()")
	public void doafter() {
		System.out.println("我是最终通知。");
	}
	
	//异常通知
	@AfterThrowing(pointcut="anyMethod()", throwing="ex")
	public void doexception(Exception ex) {
		System.out.println("我是异常通知: " + ex);
	}
	
	//环绕通知
	@Around("anyMethod()")
	public Object doprocess(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("我是环绕--进。");
		Object o = pjp.proceed();
		System.out.println("我是环绕--出。");
		return o;
	}
	
}
 

 

 

 

 

分享到:
评论

相关推荐

    spring aop实例annotation方法实现

    NULL 博文链接:https://bijian1013.iteye.com/blog/2142528

    spring aop xml 实例

    aop入门。

    SpringAOP的注解配置

    SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo

    spring 2.0使用AOP实例(基于Annotation的配置方式)

    NULL 博文链接:https://baobeituping.iteye.com/blog/1201798

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    spring中自定义注解(annotation)与AOP中获取注解

    spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。

    详解Spring Aop实例之AspectJ注解配置

    本篇文章主要介绍了详解Spring Aop实例之AspectJ注解配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring的学习笔记

    (三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 (七) Pointcut 26 (八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:...

    spring2.5 学习笔记

    (三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 (七) Pointcut 26 (八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:...

    Android AOP 注解详解及简单使用实例(三)

    Android AOP 注解详解及简单使用实例(三) 一、简介 在Android 里面 注解主要用来干这么几件事: 和编译器一起给你一些提示警告信息。 配合一些ide 可以更加方便快捷 安全有效的编写Java代码。谷歌出的support-...

    Spring和Hibernate学习笔记

    Hibernate: 第一课:第一个hibernate项目 第二课:测试实体对象的生命周期 第三课:hibernate基本映射 ...第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP的只是(采用配置文件的方式)

    Spring中文帮助文档

    2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 ...

    Spring API

    2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 ...

    Spring.3.x企业应用开发实战(完整版).part2

    14.4.4 Spring OXM 简单实例 14.5 小结 第15章 Spring MVC 15.1 Spring MVC概述 15.1.1 体系结构 15.1.2 配置DispatcherServlet 15.1.3 一个简单的实例 15.2 注解驱动的控制器 15.2.1 使用@RequestMapping映射请求 ...

    Bee事务注解@Tran使用实例工程

    Bee事务注解@Tran使用实例工程; 基于Spring boot + Bee; 涉及spring aop, 注解拦截,应该有类上和应该在方法的注解拦截. @within,@annotation使用实例.

    Spring3.x企业应用开发实战(完整版) part1

    14.4.4 Spring OXM 简单实例 14.5 小结 第15章 Spring MVC 15.1 Spring MVC概述 15.1.1 体系结构 15.1.2 配置DispatcherServlet 15.1.3 一个简单的实例 15.2 注解驱动的控制器 15.2.1 使用@RequestMapping映射请求 ...

    SSH2框架搭建实例

    3、struts2的配置采用xml(目前不支持annotation),spring和hibernate使用annotation配置; 4、开发者下载该应用后,只需修改WebRoot\WEB-INF\applicationContext.xml,将其中数据库连接信息修改为自己的即可;

    spring_MVC源码

    本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...

    AutoLoadCache:AutoLoadCache是​​基于AOP + Annotation等技术实现的高效的缓存管理解决方案,实现缓存与业务逻辑的解压缩,并增加异步刷新及“拿来主义机制”,以适应高并发环境下的使用

    然后我们就使用AOP + Annotation来解决这个问题,同时使用自动加载机制来实现数据“常驻内存”。 在infoq发表的文章 推荐使用这个, 中也有可运行的例子。 原始阅读 已经实现基于aspectj的AOP,代码在。想通过阅读...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

Global site tag (gtag.js) - Google Analytics