Spring AOP framework and the use of personal understanding of the underlying principles

Spring AOP framework and the use of personal understanding of the underlying principles Spring AOP framework and the use of personal understanding of the underlying principles****

Preface:

AOP is currently one of the core Spring framework, has a very important role in the application, is also the basis for other Spring components. It is an idea-oriented programming section. About AOP basics, I believe that the majority of children's shoes are already well aware, we need to give today is to share the use of Spring AOP framework, as well as some of my personal understanding of the underlying principles.

Aop use the steps

Aop configuration information

<Aop: config> root node corresponds aop

Configuration entry point

<Aop: piontcut> entry points need to be understood that the method of strengthening a position

Such as:

execution( com.mmr.entity.People.(..) )

This position represents the starting point in com.mmr.entity.People this class;

The first asterisk: that access to any, can also be set to private, protected. . . . .

The second asterisk: People like to represent all of the following methods

(..) represents any parameters

Points represent two classes following packet contains the following sub-packets, the sub-packets do not contain a single point

Configuration cut surface

<aop:aspect>

ref specifies what classes to enhance, points to a bean

Configuration cut time

<Aop: after> ...... such labels specified cut-in time

after --- the equivalent of try catch in the final finally

after-returning post-process performed after no abnormal

It specifies the method by which method to enhance (Method in ref pointed to class)

pointcut-ref specify an enhanced entry point

Configuration example:

<bean name="people" class="com.mmr.entity.People"></bean>

<bean name = "add" class = "com.mmr.entity.AddMethod"/>

<aop:config>

<aop:pointcut expression="execution( com.mmr.entity.People.(..) )" id="peoplecut"/>

<aop:aspect ref="add">

<aop:after method="after" pointcut-ref="peoplecut"/>

<aop:before method="before" pointcut-ref="peoplecut"/>

<aop:around method="around" pointcut-ref="peoplecut"/>

<aop:after-throwing method="throwing" pointcut-ref="peoplecut"/>

<aop:after-returning method="after_return" pointcut-ref="peoplecut"/>

</aop:aspect>

</aop:config>

Note :

the bottom spring Aop using dynamic proxy (JDK dynamic proxy + cglib own dynamic agent) way to implement enhanced

So if the enhanced class (delegate class) implements the interface, jdk own dynamic proxy approach will be adopted, it is necessary to use the interface to receive, you can not use the implementation class received directly!

If the delegate does not implement the interface, then using dynamic proxy cglib manner, can be received directly by the delegate class

Like this article, you can point to the author likes to point at attention, will share Java-related articles every day!

Remember to focus on me oh, will occasionally presented benefits, including consolidation of interview questions, learning materials, source code, etc. ~ ~

Guess you like

Origin blog.51cto.com/14456091/2425212