spring的AOP是基于代理的。java的事务控制是基于代理的。这两句话熟悉吧,一些老鸟就喜欢用这些词汇让我们犯晕,让我们感到他们很专业。这里我们将打破这个神话,本篇先介绍基于代理的AOP。这里我们先展示下什么叫做代理,先从静态代理开始。
一个静态代理的例子。
1、定义一个接口
AOP之静态代理
package com.itcast.aop;
//要实现的接口
public interface IHello {
public void hello(String name) throws Exception;
public void helloAaa(String name);
public void helloBbb(String name);
}
2、一个实现接口的实例
package com.itcast.aop;
//实现方法1
public class HelloSpeaker implements IHello{
// private Logger logger = Logger.getLogger(this.getClass().getName());
public void hello(String name) throws Exception{
System.out.println("Hello "+ name);
}
public void helloAaa(String name) {
System.out.println("Hello in aaa "+ name);
}
public void helloBbb(String name) {
System.out.println("Hello in bbb "+ name);
}
}
3、调用的静态代理类
package com.itcast.aop;
import java.util.Date;
import java.util.logging.Logger;
//静态代理类
public class HelloProxy implements IHello {
private Logger logger = Logger.getLogger(this.getClass().getName());
private IHello helloObj;
public HelloProxy(IHello helloObj){
this.helloObj=helloObj;
}
public void hello(String name) {
logger.info("method start at "+new Date());
try {
helloObj.hello(name);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("method end at "+new Date());
}
public void helloAaa(String name) {
System.out.println("Hello in aaa"+ name);
}
public void helloBbb(String name) {
System.out.println("Hello in bbb"+ name);
}
//静态代理类的测试方法
public static void main(String[] args){
IHello proxy = new HelloProxy(new HelloSpeaker());
try {
proxy.hello("tester");
} catch (Exception e) {
e.printStackTrace();
}
}
}
上边就是我们的静态代理类,代理了我们IHello接口的实现类,并加上了自己的日志输出,在IHello的实现类的hello方法每次被调用的时候,都会被在其前后加上日志。
结果如下:
信息: method start at Sun Mar 15 10:57:31 CST 2009
2009-3-15 10:57:32 com.itcast.aop.HelloProxy hello
信息: method end at Sun Mar 15 10:57:32 CST 2009
以上就是静态代理,原理很简单,目的也很简单,就是调用接口实现类的某个方法之前或者之后调用另外的操作。
总结下开发步骤:
1、创建一个接口
2、创建一个接口的实现类
3、创建一个代理类,这个代理类也实现了上边的接口,但是将 要被代理的实现类传入到代理类中,在代理类的接口实现方法中调用,并在调用前后加上一些操作(上边的就是日志操作而已)
分享到:
相关推荐
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
SpringFramework5 包含响应流(定义响应性API的语言中立尝试)和 Reactor(由Spring Pivotal团队提供的 Reactive Stream 的Java实现), 以用于其自身的用途以及其许多核心API。 Spring Web Reactive 在 spring-...
Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...
5. 验证、数据绑定和类型转换 6. Spring表达式语言 9. Spring框架下的测试 10. 单元测试 11. 集成测试 14. DAO支持 15.使用JDBC实现数据访问 16. ORM和数据访问 17. 使用 O/X(Object/XML)映射器对XML进行编组 19. ...
springframework5Courseproject:Spring Framework 5课程项目
spring framework 中文参考手册
org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....
Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
spring5 框架 中文文档_spring-framework-5-doc-cn.zip
Spring Framework 开发库, 2019年5月9日最新发布 以下是5.1.7.release, 包括主要组件: spring-aop spring-aspects spring-beans spring-context spring-context-indexer spring-context-support spring-core spring-...
org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...
spring5webapp:这是适用于Spring Framework 5和Spring Boot 2的最现代,最全面的课程。本课程中使用的所有源代码示例都是使用最新版本的Spring Framework开发的-Spring Framework 5和Spring Boot 2。您将使用Spring ...
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
Spring Framework 4.x Reference Documentation 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...
Spring Framework 4.0.2.RELEASE API,该文档来自官网经本人制作而成方便使用的chm格式。
org.springframework.core.jar
This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...