`
flyingsword945
  • 浏览: 24310 次
  • 性别: Icon_minigender_2
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

spring in action 2.0读书笔记(四)

阅读更多
Advice defines both the what and the when of an aspect.
A joinpoint is a point in the execution of the application where an aspect can be plugged in.
If advice defines the what and when of aspects then pointcuts define the where. A pointcut definition matches one or more joinpoints at which advice should be woven.
An aspect is the merger of advice and pointcuts.
An introduction allows you to add new methods or attributes to existing classes.
A target is the object that is being advised.
A proxy is the object created after applying advice to the target object.
Weaving is the process of applying aspects to a target object to create a new, proxied object.
Compile time—Aspects are woven in when the target class is compiled. This requires a special compiler. AspectJ’s weaving compiler weaves aspects this way.
Classload time—Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class’s bytecode before the class is introduced into the application. AspectJ 5’s load-time weaving (LTW) support weaves aspects in this way.
Runtime—Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects. This is how Spring AOP aspects are woven.
Spring does not create a proxied object until that proxied bean is needed by the application. If you are using an ApplicationContext, the proxied objects will be created when it loads all of the beans from the BeanFactory. Because Spring creates proxies at runtime, you do not need a special compiler to weave aspects in Spring’s AOP.

Spring generates proxied classes in two ways.
If your target object implements an interface(s) that exposes the required methods, Spring will use the JDK’s java.lang.reflect.Proxy class. This class allows Spring to dynamically generate a new class that implements the necessary interfaces, weave in any advice, and proxy any calls to these interfaces to your target class.
If your target class does not implement an interface, Spring uses the CGLIB library to generate a subclass to your target class. When creating this subclass, Spring weaves in advice and delegates calls to the subclass to your target class.

Spring only supports method joinpoints.
AspectJ and JBoss, which provide field and constructor joinpoints in addition to method pointcuts.

Advice type -- Interface
Before -- org.springframework.aop.MethodBeforeAdvice
After-returning -- org.springframework.aop.AfterReturningAdvice
After-throwing -- org.springframework.aop.ThrowsAdvice
Around -- org.aopalliance.intercept.MethodInterceptor
Introduction -- org.springframework.aop.IntroductionInterceptor
Around advice also offers you the opportunity to inspect and alter the value returned from the advised method. This makes it possible to write advice that performs some postprocessing on a method’s return value before returning the value to the caller. AfterReturningAdvice only allows you to inspect the returned value—you can’t change it.
<bean id="audienceAdvisor"
class="org.springframework.aop.aspectj.
➥ AspectJExpressionPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="expression" value="execution(* *.perform(..))" />
</bean>
<bean id="duke"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="dukeTarget" />
<property name="interceptorNames" value="audienceAdvisor" />
<property name="proxyInterfaces"
value="com.springinaction.springidol.Performer" />
</bean>

Spring comes with a handy implementation of BeanPostProcessor called DefaultAdvisorAutoProxyCreator, which automatically checks to see whether an advisor’s pointcut matches a bean’s methods and replaces that bean’s definition with a proxy that applies the advice. In a nutshell, it automatically proxies beans with matching advisors.
<bean class="org.springframework.aop.framework.autoproxy.
➥ DefaultAdvisorAutoProxyCreator" />

Autoproxying @AspectJ aspects
<aop:aspectj-autoproxy/> will create an AnnotationAwareAspectJAutoProxy- Creator in the Spring context and will automatically proxy beans whose methods match the pointcuts defined with @Pointcut annotations in @Aspectannotated beans.
Declaring a pure-POJO to aspects
<aop:config>
<aop:aspect ref="audience">
<aop:before
method="takeSeats"
pointcut="execution(* *.perform(..))" />
<aop:before
method="turnOffCellPhones"
pointcut="execution(* *.perform(..))" />
<aop:after-returning
method="applaud"
pointcut="execution(* *.perform(..))" />
<aop:after-throwing
method="demandRefund"
pointcut="execution(* *.perform(..))" />
</aop:aspect>
</aop:config>

Injecting AspectJ aspects
public aspect JudgeAspect {
public JudgeAspect() {}
pointcut performance() : execution(* perform(..));
after() returning() : performance() {
System.out.println(criticismEngine.getCriticism());
}
....
}

<bean class="com.springinaction.springidol.JudgeAspect"
factory-method="aspectOf">
<property name="criticismEngine" ref="criticismEngine" />
</bean>

Spring doesn’t use the <bean> declaration from earlier to create an instance of the JudgeAspect—it has already been created by the AspectJ runtime. Instead, Spring retrieves a reference to the aspect through the aspectOf() factory method.
分享到:
评论

相关推荐

    Struts2.0培训笔记

    本培训笔记将深入探讨Struts2.0的核心概念、特性以及如何在实际项目中有效应用。 一、Struts2.0框架基础 Struts2.0是Apache软件基金会的项目,它是Struts1.x的升级版,提供了更强大的功能和更好的性能。该框架通过...

    strust2.0学习笔记

    ### Struts2.0 学习笔记 #### 引言 Struts2 是一款非常流行的 Java Web 开发框架,它基于 Struts1 进行了重大的改进与优化,不仅继承了 Struts1 的优秀特性,还在此基础上进行了扩展,支持更加丰富的功能,如拦截...

    Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记

    Struts 2、Spring 2.0 和 Hibernate 3.0 是Java开发中经典的MVC框架组合,它们各自负责不同的职责,共同构建了一个强大的企业级应用架构。在本笔记中,我们将深入探讨这三个框架的整合过程及其核心概念。 **Struts ...

    Struts2.0学习笔记.doc

    Struts2.0是一个流行的Java Web开发框架,它极大地简化了MVC(模型-视图-控制器)架构的实现。在Struts2中,类型转换是自动进行的,旨在帮助开发者处理请求参数与Action类属性之间的类型匹配问题。文档中的内容主要...

    struts2.0笔记(1)

    总的来说,Struts2.0笔记将涵盖MVC设计模式的实现、Action的定义与调用、拦截器的应用、配置文件的解析、结果处理和视图展现等方面的知识。通过学习Struts2.0,开发者可以构建出结构清晰、易于维护的Web应用,同时也...

    Struts2.0笔记

    Struts2.0 是一款基于 MVC 设计模式的开源框架,用于构建企业级的 Java Web 应用。它简化了MVC开发,提供了一种更简单、更灵活的方式来处理请求和响应。以下是对Struts2.0核心概念的详细解释: 1. **运行环境配置**...

    Struts2 + Spring + Hibernate + DWR 项目布署笔记

    在`struts2-spring-plugin-2.0.11.2.jar`中,包含了Struts2与Spring集成所需的类和配置,帮助管理Struts2的Action实例。 其次,Spring框架是Java开发的核心工具,它不仅提供了DI和AOP,还支持事务管理、数据访问...

    struts2.0(希望可以帮助大家)

    这个压缩包包含的资源是作者学习Struts2.0过程中的笔记和示例,分为两个文档:struts2.0文档1-2.doc和struts2.0文档3-4-5.doc,涵盖了从基础到进阶的内容。 在"struts2.0文档1-2.doc"中,可能包括了以下知识点: 1...

    ssh笔记及jar包

    SSH(Struts+Spring+Hibernate)是一个经典的Java Web开发框架,它将Struts的MVC设计模式、Spring的依赖注入和事务管理以及Hibernate的对象关系映射整合在一起,为开发人员提供了一种高效、灵活的开发环境。这篇笔记...

    Java/JavaEE 学习笔记

    第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 PL/SQL程序结构................359 第三章 PL/...

    ssh整合学习笔记(图解)

    3. **Type属性值的设置**:在Struts配置文件中,将Action的type属性值设置为`SpringProxyAction`,这样,每当有请求到达时,Struts会自动调用Spring容器,由Spring负责实例化并调用具体的Action类,实现了框架之间的...

    J2EE学习笔记(J2ee初学者必备手册)

    第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 PL/SQL程序结构................359 第三章 PL/SQL...

    j2ee SSH 整合笔记,献于新手。。

    - **修改`struts-config.xml`**:为了使用Spring管理Struts的Action,需要将原有的Action配置修改为使用Spring的代理Action。例如: ```xml &lt;action name="IndexActionForm" path="/indexAction" scope="request...

    Struts笔记

    - **易于集成**: Struts 可以很容易地与其他 Java EE 技术(如 Hibernate 和 Spring)进行集成。 - **灵活的配置**: 通过 XML 文件进行配置,使得开发者可以根据实际需求调整框架的行为。 #### 三、Struts的安装与...

    Strtus2学习笔记

    ### Struts2学习笔记知识点梳理 #### 一、前言及背景 - **Struts2简介**:Struts2是一个基于MVC模式的开源Web应用框架,它继承了Struts1的一些特性,并在此基础上进行了很多改进,使得开发更加便捷高效。 - **学习...

    struts2资料可做参考

    "struts2学习笔记(一) ——struts2与spring2_0的集成 - 一嗑立扑死 - CSDN博客.mht"可能详细讨论了如何将Struts2与Spring 2.0版本集成,包括Action的配置和依赖注入的使用。而"Struts2与Spring的结合 - Naviler的...

    struts学习笔记

    - **集成能力**:Struts2很容易与其他框架(如Spring、Hibernate等)集成。 **Struts2的下载**: - 官方网站:[http://www.apache.org/download](http://www.apache.org/download) - 下载包结构:Struts2-1.6.zip ...

Global site tag (gtag.js) - Google Analytics