`

Spring.AOP下的 hello world !

阅读更多
此方法只做演示,项目中不使用
项目中用annotation

此代码输出 "World"
package com.test532;

public class MessageWriter {
	public void writeMessage(){
		System.out.print("World");
	}
}


如何使用AOP,在松耦合情况下输出 hello world !

以下代码实现:


package com.test532;

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

//此类实现包围通知  MethodInterceptor
public class MethodDecorator implements MethodInterceptor{

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


public class HelloWorldWriter {
	public static void main(String[] args) {
		MessageWriter writer = new MessageWriter();
		//使用ProxyFactory是完全编程式的做法,一般我们不需要这么做,
		//可以依靠ProxyFactoryBean类来声明式地创建代理 
		ProxyFactory factory = new ProxyFactory();//创建代理工厂
		factory.addAdvice(new MethodDecorator());//织入通知
		factory.setTarget(writer);//设定织入的目标对象
		MessageWriter proxy = (MessageWriter)factory.getProxy(); //获得代理
		proxy.writeMessage();
	}
}


打印出:
Hello World !


分享到:
评论

相关推荐

    Spring的AOP示例DEMO HELLOWORLD

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

    Spring入门笔记.md

    ## Spring入门学习 首先认识下Spring的结构 ![架构图]...<bean id="helloBean" class="mybatis.study.start.bean.HelloWorld"> ``` list Map,provincecitymysqq

    hello Spring

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

    跟开涛学Spring

    1.19 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . .208 http://jinnianshilongnian.iteye.com 第 2 / 366 页 1.20 【第六章】 AOP 之 6.3 基于...

    SpringMVC3.1.2 入门级HelloWorld源码

    [INFO] | \- org.springframework:spring-aop:jar:3.1.2.RELEASE:compile [INFO] +- org.springframework:spring-core:jar:3.1.2.RELEASE:compile [INFO] | +- org.springframework:spring-asm:jar:3.1.2.RELEASE:...

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

    2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在...

    Spring的Hello World:理解AOP

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

    Spring-Reference_zh_CN(Spring中文参考手册)

    使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.2.1. PropertyPlaceholderConfigurer示例 3.7.2.2. ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring3.1中文参考文档

    1.2.4 AOP和基础组件...............................................................................................................................9 1.2.5 测试.............................................

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

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

    跟我学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 切面实例化模型

    Java 面试宝典

    s = s + " world!";这两行代码执行后,原始的 String 对象中的 内容到底变了没有? ..................................................................................................... 23 32、是否可以继承...

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

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

    跟我学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...

Global site tag (gtag.js) - Google Analytics