`

AOP之Hello World

    博客分类:
  • java
AOP 
阅读更多

 目标

package com.aop.joinpoint;

public class Greeting {

	public void greet() {
		System.out.print("World");
	}
	
	public String greetWithParam(String param) {
		System.out.println("Param is " + param);
		return param + ", nice to see you!";
	}

}

 

 环绕通知(又叫包围通知)

package com.aop.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.print("Hello ");
		Object ret = invocation.proceed();
		System.out.println("!");
		return ret;
	}

}

 

前置通知

package com.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] arg, Object target) throws Throwable {
		if (arg[0] == null) {
			throw new IllegalArgumentException("arg cannot be null.");
		}
		System.out.println("Before method:" + method.getName() + "(\"" + arg[0] + "\")");
	}

}

 

后置通知

package com.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object returningValue, Method method, Object[] arg, Object target) throws Throwable {
		System.out.println("After method:" + method.getName() + "(\"" + arg[0] + "\")");
	}

}

 

手动创建代理

package com.aop.demo;

import org.springframework.aop.framework.ProxyFactory;

import com.aop.advice.AfterAdvice;
import com.aop.advice.AroundAdvice;
import com.aop.advice.BeforeAdvice;
import com.aop.joinpoint.Greeting;

public class GreetingDemo {

	public static void main(String[] args) {
		Greeting target = new Greeting();
		AroundAdvice aroundAdvice = new AroundAdvice();
		BeforeAdvice beforeAdvice = new BeforeAdvice();
		AfterAdvice afterAdvice = new AfterAdvice();

		ProxyFactory pf = new ProxyFactory();
		pf.addAdvice(aroundAdvice);
		pf.setTarget(target);

		Greeting proxy = (Greeting)pf.getProxy();

		//不使用代理
		target.greet();
		System.out.println();

		//使用代理(包围通用)
		proxy.greet();

		System.out.println();
		pf.removeAdvice(aroundAdvice);
		pf.addAdvice(beforeAdvice);
		proxy = (Greeting)pf.getProxy();

		//使用代理(前置通用)
		System.out.println(proxy.greetWithParam("Bruce"));

		System.out.println();
		pf.removeAdvice(beforeAdvice);
		pf.addAdvice(afterAdvice);
		proxy = (Greeting)pf.getProxy();

		//使用代理(后置通用)
		System.out.println(proxy.greetWithParam("David"));
	}

}

 

输出

/**Output:
World
Hello World!

Before method:greetWithParam("Bruce")
Param is Bruce
Bruce, nice to see you!

Param is David
After method:greetWithParam("David")
David, nice to see you!
*/

 

分享到:
评论

相关推荐

    Spring的AOP示例DEMO HELLOWORLD

    根据学习笔记整理的HelloWorld,需要自行下载Spring3相关的包

    SpringMVC3.1.2 入门级HelloWorld源码

    [INFO] | +- aopalliance:aopalliance:jar:1.0:compile [INFO] | +- org.springframework:spring-beans:jar:3.1.2.RELEASE:compile [INFO] | \- org.springframework:spring-context:jar:3.1.2.RELEASE:compile ...

    Spring的Hello World:理解AOP

    NULL 博文链接:https://istone.iteye.com/blog/423895

    跟我学spring3(1-7).pdf

    —— 5.1 概述 5.2 SpEL基础5.3 SpEL语法5.4在Bean定义中使用EL6.1 AOP基础6.2 AOP的HelloWorld6.3 基于Schema的AOP6.4 基于@AspectJ的AOP 6.5 AspectJ切入点语法详解6.6 通知参数6.7 通知顺序6.8 切面实例化模型

    helloworld.zip

    springboot项目中怎么使用aop全局拦截controller的url以及参数;拦截器的使用等示例demo;springbootAop的应用可以使开发者很方便的记录日志和用户操作记录;方便了程序查错和分析性能的重要的一个方法。 能学到什么...

    AOP in .net

    AOP in .NET is a great resource for those interested in learning how aspects can ...这本书由浅入深,有hello world 的入门案例,还有从没有AOP到AOP的重构对比,也有深入的AOP方方面面。想成为AOP高手的人必读。

    Java Framework 关于IOC、AOP、Log的案例源码

    该源码是课程 Java Spring案例精讲 ---- Spring框架 的源码,包含Java Spring的最简单的Hello World、IOC、AOP及Log的源码 Spring整体框架中的核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等...

    hello Spring

    下面是Spring的HelloWorld的程序的文件结构: C:. │ .classpath │ .project │ ├─build │ └─classes │ └─com │ ├─dineshonjava │ │ └─sdnext │ │ └─springConfig │ │ spring.xml │ │ │ ...

    跟我学spring3(1-7)

    【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我学spring3 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3 【第六章】 AOP 之 6.5 AspectJ...

    JBoss ESB 学习笔记

    2——第一个ESB代码Hello World 12 3——第二个ESB代码Hello World Action 29 4——第三个ESB代码Hello World Notification 38 5——第四个ESB代码Hello World File Action 45 6——第五个ESB代码Custom Action 51 7...

    SpringFramework中的AOP编程之入门篇

    使用跟踪和记录方面(面向方面领域的HelloWorld)作为例子,本文展示了如何使用Spring框架所独有的特性来声明切入点和通知以便应用方面。本系列的第二部分将更深入地介绍如何运用Spring中的所有通知类型和切入点来...

    SpringFramework中的面向方面编程(AOP),第二部分

    火龙果软件工程技术中心 在本系列的第一部分,我介绍了如何实现面向方面领域的“HelloWorld”:跟踪和记录方面。利用Spring框架所提供的面向方面编程(Aspect-OrientedProgramming,AOP)功能,您看到了如何使用...

    SSH(Struts1.0+Spring+Hibernate)框架集成笔记

    SSH框架集成是较复杂和难理解的,只有在不断的练习和使用中才能慢慢的理解其中的原理,仅凭看视频是远远不够的,因为这些涉及了尤其是spring底层的好多类以及控制翻转(IOC)和面向切面(AOP)编程的思想,不过在讲述...

    SpringBoot视频教程 快速上手

    第三节:SpringBoot HelloWorld实现 第二章:SpringBoot项目属性配置 第一节:项目内置属性 第二节:自定义属性 第三节:ConfigurationProperties配置 第三章:SpringBoot之MVC支持 第一节:@RequestMapping配置url...

    Spring入门笔记.md

    ## Spring入门学习 首先认识下Spring的结构 !... 然后我们皆可以写我们的demo了 ...<bean id="helloBean" class="mybatis.study.start.bean.HelloWorld"> ``` list Map,provincecitymysqq

    发布NClay框架1.0.0.8版本

    HelloWorld 简单入门程序 ObjectBind 框架在web下的对象创建和数据绑定功能,包括实体对象、列表对象和文件上传等数据绑定获取 DataAccess 数据访问功能 Filters web下action的Filter...

    SpringFramework中的面向方面编程(AOP),第一部分

    使用跟踪和记录方面(面向方面领域的HelloWorld)作为例子,本文展示了如何使用Spring框架所独有的特性来声明切入点和通知以便应用方面。本系列的第二部分将更深入地介绍如何运用Spring中的所有通知类型和切入点来...

Global site tag (gtag.js) - Google Analytics