dependency injection and aop n.
Skip this Video
Loading SlideShow in 5 Seconds..
Dependency Injection and AOP PowerPoint Presentation
Download Presentation
Dependency Injection and AOP

Loading in 2 Seconds...

play fullscreen
1 / 22

Dependency Injection and AOP - PowerPoint PPT Presentation

  • Uploaded on

Dependency Injection and AOP. Proxy. Limits of object-oriented programming. AOP. AOP. Advice.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Dependency Injection and AOP' - lorand

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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.

  • Spring XML 方式
  • AspectJ annotation方式
  • Schema-based 配置方式
aop in spring
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
  • 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.


1. NameMatchMethodPointcut

  • 2. RegexpMethodPointcut
  • 3. StaticMethodMatcherPointcut
  • 4. DynamicMethodMatcherPointcut
  • ComposablePointcut
  • ControlFlowPointcut

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());
  • ProxyFactorypf = new ProxyFactory();
  • pf.setTarget(target);
  • pf.addAdvisor(advisor);
  • NameMethodTargetExample proxy = (NameMethodTargetExample)pf.getProxy();
  • proxy.printName();
  • proxy.printAction();
  • proxy.printSpot();
  • }
  • }
  • 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.
  • 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
  • Before Advice
  • After Returning Advice
  • After throwing Advice
  • 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.

  • 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.

a normal advice can be applied to any object since it has a per-class lifecycle,

  • an introduction has a per-instance lifecycle.

This 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.