Spring AOP 应用模型
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://lavasoft.blog.51cto.com/62575/73438 |
Spring AOP 应用模型
一、概述
AOP是Aspect-oriented programming,中文翻译为面向切面编程。
面向切面编程(AOP)提供另外一种角度来思考程序结构,通过这种方式弥补了面向对象编程(OOP)的不足。
Spring的一个关键的组件就是 AOP框架。 尽管如此,Spring IoC容器并不依赖于AOP,这意味着你可以自由选择是否使用AOP,AOP提供强大的中间件解决方案,这使得Spring IoC容器更加完善。
Spring中所使用的AOP:
这样你可以把Spring AOP看作是对Spring的一种增强,它使得Spring可以不需要EJB就能提供声明式事务管理; 或者也可以使用Spring AOP框架的全部功能来实现自定义的切面。
二、AOP术语
首先让我们从定义一些重要的AOP概念开始。这些术语不是Spring特有的。 不幸的是,Spring术语并不是特别的直观;如果Spring使用自己的术语,将会变得更加令人困惑。
通知的类型:
环绕通知是最常用的一种通知类型。大部分基于拦截的AOP框架,例如Nanning和JBoss4,都只提供环绕通知。
三、给个最简单的Demo看看
/** * Created by IntelliJ IDEA.<br> * <b>User</b>: leizhimin<br> * <b>Date</b>: 2008-4-21 10:19:15<br> * <b>Note</b>: 公共接口,代理类和被代理类都实现此业务接口 */ public interface IHello { public void hello(String name); } /** * Created by IntelliJ IDEA.<br> * <b>User</b>: leizhimin<br> * <b>Date</b>: 2008-4-21 10:19:47<br> * <b>Note</b>: 被代理类 */ public class HelloSpeaker implements IHello { public void hello(String name) { System.out.println("Hello, " + name); } } /** * Created by IntelliJ IDEA.<br> * <b>User</b>: leizhimin<br> * <b>Date</b>: 2008-4-21 10:44:39<br> * <b>Note</b>: 测试类,客户端 */ public class SpringAOPDemo { public static void main(String args[]){ // ApplicationContext context = BeanContextHelper.getApplicationContext(); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IHello helloproxy = (IHello)context.getBean("helloProxy"); helloproxy.hello("lavasoft"); } } /** * Created by IntelliJ IDEA.<br> * <b>User</b>: leizhimin<br> * <b>Date</b>: 2008-4-21 10:22:22<br> * <b>Note</b>: 在被代理方法执行前添加的动作 */ public class LogBeforeAdvice implements MethodBeforeAdvice { private Logger logger = Logger.getLogger(this.getClass().getName()); public void before(Method method, Object[] objects, Object o) throws Throwable { logger.log(Level.INFO, "method start..." + method); } } <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="logBeforeAdvice" class="com.lavasoft.springnote.ch02.aoptest.LogBeforeAdvice"/> <bean id="helloSpeaker" class="com.lavasoft.springnote.ch02.aoptest.HelloSpeaker"/> <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.lavasoft.springnote.ch02.aoptest.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>LogBeforeAdvice</value> </list> </property> </bean> </beans> 运行结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory). log4j:WARN Please initialize the log4j system properly. 2008-4-25 9:25:31 com.lavasoft.springnote.ch02.aoptest.LogBeforeAdvice before 信息: method start...public abstract void com.lavasoft.springnote.ch02.aoptest.IHello.hello(java.lang.String) Hello, lavasoft Process finished with exit code 0 四、Advice层次结构
org.aopalliance.aop.Advice
Advice 是在切面的某个特定的连接点上执行的动作。
Advice是一个标识接口,有很多的子接口。BeforeAdvice就是在被代理方法调用前所执行的接口动作。
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/73438 本文出自 51CTO.COM技术博客 |



leizhimin
博客统计信息
热门文章
最新评论
友情链接
