Java Interview--spring Technical Essentials--spring AOP (plane-oriented programming)

Source: Internet
Author: User

the concept of Spring AOP

Aspect-oriented programming is a programming technique that complements and perfects oop (object-oriented programming). The execution of OOP is a process from the top down, and there is no relationship from left to right. So in OOP programming, there will be a lot of duplicate code. AOP, in turn, extracts these business-independent duplicate code and then embeds it into the business code. Common applications include: Rights Management, logging, transaction management, and so on.

The implementation of AOP technology, mainly divided into two categories: first, the use of dynamic Agent technology, the use of interception of messages to decorate the message to replace the original object behavior, the second is the use of static weaving, the introduction of a specific syntax to create "aspects", so that the compiler can be woven into the relevant "aspects" The code. Spring AOP The implementation uses a dynamic proxy approach.

http://blog.csdn.net/moreevan/article/details/11977115

related concepts of AOP

  • facets / aspect (Aspect): The AOP core is the facet, which encapsulates the common behavior of multiple classes into reusable modules, The module contains a set of APIs that provide crosscutting functionality. For example, a log module can be called an AOP facet of a log. Depending on the requirements, an application can have several facets. In SPRINGAOP, facets are implemented by classes with @aspect annotations.
  • connection point (joinpoint): An explicit point during program execution, such as a call to a method or a particular exception being thrown.
  • notification / enhancement (Advice): On the pointcut, the enhancements that can be applied include: around, Before and throws. Many of the AOP frameworks, including spring, are notification models with interceptors, maintaining a chain of interceptors around the connection point. Four Advice:beforeadvice, Afteradvice, Throwadvice and dynamicintroductionadvice are defined in spring.
  • entry point ( Pointcut ) : The collection of connection points ( usually the Method collection ) that will be enhanced (Advice) applied. Spring defines the Pointcut interface for combining Methodmatcher and Classfilter, which can be clearly understood by name, and Methodmatcher is used to check whether the method of the target class can be applied to this notification. The classfilter is used to check whether the pointcut should be applied to the target class.
  • target object (TargetObject): Notified (Advice) or Proxied object.
  • AOP proxy: An AOP framework-created object that contains notifications (Advice). In spring, an AOP proxy can be either a JDK dynamic agent or a cglib proxy.

Enhanced/notification (Advice) type for Spring AOP

    1. before Advice: executes before the method executes.
    2. afteradvice: A notification that is invoked after the method executes, regardless of whether the method execution succeeds.
    3. After returningadvice: executes after a result is returned after the method executes.
    4. After throwingadvice: executes when an exception is thrown during method execution.
    5. Around Advice : Executes before and after the execution of the method and throws an exception, which is the equivalent of synthesizing the three notifications. (Related interface methodintercept)
    6. Introductionadvice( Introduction Enhancement ): The introduction of notifications is a special kind of notification that can introduce new member variables and member methods into the target class. It cannot act on any pointcuts, as it is used only for class hierarchies, not method hierarchies. Implementing the introduce notification requires implementing the Introductionadvisor and Introductioninterceptor interfaces.

http://chenjumin.iteye.com/blog/364948

The differences between the concerns and crosscutting concerns of Spring AOP

    • The Focus is the behavior of a module in the application, and a focus may be defined as a function that we want to implement.
    • crosscutting concerns are a concern that is used by the entire application and affects the entire application, such as logging, security, and data transfer, and the functionality required for almost every module that is applied. So these are all crosscutting concerns.

http://blog.csdn.net/shendl/article/details/526362

37 Introduction (Introduction) Concept

Introduce (Introduction): Add a method or field to the class being notified. Spring allows the introduction of new interfaces to any notified object. For example, you can simplify caching by using an introduction that enables any object to implement the IsModified interface. To use introduction in spring, you can implement notifications through Delegatingintroductioninterceptor. Configure the interfaces to be implemented by the advice and proxy classes through Defaultintroductionadvisor.

Spring has several automatic proxies

There are three types of proxies:

    • Automatic proxy creator based on Bean's name, e.g. beannameautoproxycreator
    • An automatic proxy creator based on the advisor (tangent) matching mechanism. Scans all the advisors in the spring container and applies them to the matching bean. such as defaultadvisorautoproxycreator
    • An automatic proxy creator based on the ASPJECTJ annotation label in the bean, such as annotationawareaspectjautoproxycreator

All of the automatic proxy creators are implemented with Beanpostprocessor. When the spring container instantiates the bean, beanpostprocessor will process it and automatically create a proxy object for the bean that satisfies the matching rule.

http://blog.csdn.net/itomge/article/details/8861268

http://uule.iteye.com/blog/894055

Spring Weaving Concept

Weaving (Weaving): The process of applying a facet (Aspect) to a target object to create a new proxy object, which generally occurs in the following time:

    1. compile time : When a class file is compiled, this requires a special compiler to do so, such as AspectJ's weaving compiler.
    2. class loading : Use special ClassLoader to enhance the class's byte code before the target class is loaded into the program.
    3. runtime : The slice is woven at some point in the run, and the SPRINGAOP is woven into the plane in this way, the principle should be the use of dynamic agent technology.

how Spring AOP is implemented1.The Classic agent-basedAOP: Using Java code implementations, write advice, PointCut, and then provide it to the advisor. After you turn on automatic proxy, you can get the enhanced bean in ApplicationContext.

Http://blog.sina.com.cn/s/blog_5198c7370100hw1p.html

2,@AspectJ annotation-driven facets : annotation-based development (recommended), in the project need to open the AOP automatic proxy <aop:aspectj-autoproxy/>.

3,XML Schema mode: need to implement the corresponding enhanced interface, such as Beforeadvice, Afteradvice and so on. Then use the configuration as:

<aop:config>

<aop:aspectref= "Sleephelper" >

<aop:beforemethod= "Beforesleep" pointcut= "Execution (**.sleep (..))" />

<aop:aftermethod= "Aftersleep" pointcut= "Execution (**.sleep (..))" />

</aop:aspect>

</aop:config>

http://hyhai7.iteye.com/blog/837497


Java Interview--spring Technical Essentials--spring AOP (plane-oriented programming)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.