dependency injection and aop
DESCRIPTION
Dependency Injection and AOP. Proxy. Limits of object-oriented programming. AOP. AOP. Advice. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/1.jpg)
DEPENDENCY INJECTION ANDAOP
![Page 2: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/2.jpg)
Proxy
![Page 3: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/3.jpg)
Limits of object-oriented programming
•问题Code scattering
•Blocks of duplicated code •Blocks of complementary code, and different modules implementing complementary parts of the concern
Code tangling •Code tangling occurs when a module has to manage several concerns at the same time such as logging, exception handling, security, caching, and more or when a module has elements of the implementation of other concerns inside.
![Page 4: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/4.jpg)
AOP 面向对象Aspect ClassJoinpoint This is the application point of the aspect. It
is a point of the execution of a program such as the invocation of a constructor or the execution of a method or the management of an exception (WHEN).
Advice This is the action an aspect performs at a certain joinpoint. Advices can be "around", "before", and "after".
Pointcut This is the expression for the joinpoint's selection, for instance a method's execution with a certain signature (WHERE)
AOP
![Page 5: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/5.jpg)
AOPAOP 面向对象Introduction
This is the declaration of methods or additional fields on the object to which the aspect will be applied. It allows the introduction of new interfaces and implementations on the objects.
Target object
This is the module (Object) to which the aspect will be applied.
Weaving This is the linking action between the aspect and the objects to which advices must be applied
![Page 6: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/6.jpg)
AdviceAOP 面向对象Before advice
After returning advice Throws advice
After (finally) advice Around advice
![Page 7: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/7.jpg)
the crosscutting concerns should be analysed as a third dimension of the design. In these situations, aspect-oriented programming provides support to object-oriented programming for uncoupling modules that implement crosscutting concerns.
![Page 8: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/8.jpg)
![Page 9: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/9.jpg)
AOP定义方式 Spring XML 方式 AspectJ annotation方式 Schema-based 配置方式
![Page 10: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/10.jpg)
AOP in Spring Spring permits only method execution to be used
as a joinpoint. So we can't use with Spring AOP all the features of AOP, but we can do so with AspectJcalled by Spring. For example, we must use the support of AspectJif we want to use as a joinpoint: The invocations of constructors Access to the domains of objects with the setter and
getter The initialization of an object The initialization of an object with a calling super() The execution inside a class with this() The calling of a method
![Page 11: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/11.jpg)
JoinPoint A pointcut is an expression for the selection of
joinpoints. It can be a collection of joinpoints used to define an advice that has to be executed.
Methods starting with a certain prefix (such as, getter and setter)
Methods with a particular package (such as org.springaop.domain.*)
Methods that return a certain kind of output (such as public MyClass get*(...))
A pointcut is the composition of a ClassFilter and a MethodMatcher.
![Page 12: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/12.jpg)
1. NameMatchMethodPointcut 2. RegexpMethodPointcut 3. StaticMethodMatcherPointcut 4. DynamicMethodMatcherPointcut ComposablePointcut ControlFlowPointcut
![Page 13: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/13.jpg)
public class NameMethodMatcherExample { public static void main(String[] args) { NameMethodTargetExample target = new NameMethodTargetExample(); NameMatchMethodPointcut pc = new NameMatchMethodPointcut(); pc.addMethodName("printSpot"); pc.addMethodName("printAction"); Advisor advisor = new DefaultPointcutAdvisor(pc, new AdviceExample()); ProxyFactory pf = new ProxyFactory(); pf.setTarget(target); pf.addAdvisor(advisor); NameMethodTargetExample proxy =
(NameMethodTargetExample)pf.getProxy(); proxy.printName(); proxy.printAction(); proxy.printSpot(); } }
![Page 14: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/14.jpg)
Joinpoint A joinpoint is a well-defined point during
the execution of your application. Typical examples of joinpoints include a method call, method execution, class initialization, and object instantiation. Joinpoints are a core concept of AOP and define the points in your application at which you can insert additional logic using AOP.
Spring AOP only supports one joinpoint type—method invocation.
![Page 15: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/15.jpg)
Advice An advice specifies what must be done at
a joinpoint. Spring uses a chain of interceptors to
wrap the invocation of the method and apply to it the advices contained into this chain. As said before, Spring is consistent with the specifications of the AOP Alliance, but provides a slightly different programming model
![Page 16: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/16.jpg)
Advice
![Page 17: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/17.jpg)
Advice Before Advice After Returning Advice After throwing Advice
![Page 18: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/18.jpg)
Advisor The advisor isn't a concept from AOP in
general, but is specific to Spring AOP. The advisor links together pointcuts and
advice.So it contains both the action to be
executed (defined in the advice) and the point where it is to be executed (defined in the pointcut). The advisor's role is to decouple pointcuts and advice, in order to reuse them independently from each other.
![Page 19: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/19.jpg)
Introduction Introductions are a very strong component of
the Spring AOP. They permit us to dynamically introduce new one-object functionalities as the implementation of interfaces on existing objects:
To create a mixin, adding to the state held in the object. This is probably the most important use.
To expose additional states associated with a special TargetSource. This is used within Spring, for example, with scripting support.
To expose an object or object graph in a different way—for example, making an object graph implement the XML DOM interfaces.
![Page 20: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/20.jpg)
a normal advice can be applied to any object since it has a per-class lifecycle,
an introduction has a per-instance lifecycle.
![Page 21: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/21.jpg)
ProxyFactoryBeanThis allows us to focus our attention and development
on crosscutting concerns to apply through the proxy, rather than focus on the proxy.
setTarget: Specify the target object you want to proxy.setProxyTargetClass: The default behavior is the
following: If an interface is available, it's used as a JDK proxy. If an interface is not available, it's used as a CGLIB
proxy. If we set the value to true, it's used as a CGLIB
proxy.
![Page 22: Dependency Injection and AOP](https://reader036.vdocuments.us/reader036/viewer/2022062222/568164ad550346895dd6b2da/html5/thumbnails/22.jpg)
参考 http://www.cnblogs.com/leoo2sk/archive/
2010/11/30/aop-postsharp.html