spring-aop knowledge section

First, the concept of AOP

AOP (Aspect Oriented Programming), namely Oriented Programming. In software development, scattered in various places in the application

Function is called crosscutting concerns (cross-cutting concern), generally speaking, these cross-cutting concerns with business applications from concept

Logically separate. For example, declarative transaction, logging, security, caching, etc., have nothing to do with business logic, these things can be abstracted into

The module, by way of aspect-oriented programming, declaratively defined for these functions where, by way of pre-compiler and dynamic runtime

State agency to achieve these cross-cutting concerns modular program functions to maintain unity, so that the cross-cutting concerns with the objects they affect the

Between separated, it is decoupled.

Crosscutting concerns can be modularized into special classes, which are called facets (aspect). This has two advantages:

1) each point of interest are concentrated in one place, rather than dispersed into multiple code;

2) Services Module is more concise, because they only contain the code main point of interest (the core business logic),

The secondary focus of the code (logs, transactions, security, etc.) were transferred to the section.

Notification (Advice)

Cut class has its own work to be done, working class section is called the notification. Notification section defines what to do and when to use.

"What to do" section method that is defined in the class is doing;

"When to Use", i.e., five kinds of notification type, the method is performed before the target, or the like performed after the target method;

"Where do", that is, notice defines what to do, when to use, but do not know where, but defined cut-off point is to tell the notice should be used

On which the target method which class, which completed a perfect cross point function.

Spring section defines five types of notification:

1) Pre-notification (Before): call notification function before the target method is called.

2) After returning advice (After): call notification after completion of the target method, you do not care what method of output yes.

3) Returns the notice (After-returning): call notification after the target method is successful.

4) exception notification (After-throwing): call notification when the target method throws an exception.

5) Around advice (Around): notification methods wrapped notified, before the method is invoked and notified after the execution of custom behavior.

Section (Aspect)

Section is a combination of notice and the cut-off point, notification point and cut together define the entire contents of the section. Because notification is defined in section

"What to do" and "when to do it", and defined cut-off point is the aspect of "doing and where." The combination of the two, can be perfect

Section to show when, where, and what to do (function).

Connection point (Join point)

Method class i.e. notification may become cut points, these points are connected, then the cut is defined as the point, the connection point becomes a point of tangency, the notification type may be a class, there may be a package beneath all classes, the connection points may be referred to tens of thousands, is a virtual concept, the connection point can be set as a tangent point.

Tangent point (Poincut)

Based on the notified connection point talking about a wide range of ethereal, and tangent point is a specific position for the narrow section of the connection point notified.

As mentioned above, the definition of notice is the aspect of "what to do" and "when to do it," it is not no where to go to do while on the definition of the cut-off point "where to do."

Definition of the tangent point would match notification to be woven into one or more connection points. We usually use explicit class and method names, or use

Regular expression defines the class and method names that match specified cut point. To put it plainly, the point is to make the cut to find the notice "place to vent."

Introduction (Introduction)

The introduction of the concept is still relatively tall, introduced allows us to add a new method or property to an existing class.

The main purpose is to without modification in the A, B are introduced into the behavior and state.

Weaving (Weaving)

Weaving section is to be applied to the target object and create a new proxy object of the process. Section is woven into the target object at a specified point of attachment.

There are in the life cycle of the target object can be woven into several points:

Compile: 

    Section is woven in the target class compile time. Require special compiler, it is AspectJ way, not a spring dish.

Class load time: 

    Section is woven when the target class is loaded into the JVM. Before this approach requires special class loader, which can be incorporated in the target class applied

    Enhancement of the target class bytecode. AspectJ5 support this approach.

Runtime:  

     Section is woven into the applications running at some point. Under normal circumstances, when the weaving section, AOP container creates a dynamic target object is

     A proxy object. Spring AOP and this is the way of the weaving section.

AOP combat (annotated edition)

1, create a class section

Section class contains notice and the starting point before creating cut class, we need to understand the point of tangency expression under AspectJ, because we need to

Cut point expression defined cut-off point for accurate positioning of the notification section should be applied in any place.

 

 

 

2, create the target class that defines the target method

Instead of starting container xml embodiment @Configuration

/**

 * Jdk Agent: agent-based interface, it must be based on the interface, it will generate sub-object interface of the target object.

 * Cglib agents: based proxies, you need not be based interface, the child object that generated the target object.

 * 1. Notes @EnableAspectJAutoProxy open proxy;

 * 2. If the property proxyTargetClass default is false, the dynamic indication jdk weaving reinforcing agent;

 * 3. If proxyTargetClass property set to true, the dynamic indication Cglib weaving reinforcing agent technology;

 * 4. If the property proxyTargetClass set to false, but the target class does not declare an interface

 * Spring aop will still use Cglib dynamic proxy, that is to say non-class interface to be generated proxy are used Cglib.

 */

Use spring test class test

 

 This, we basically understand the annotation-based AOP combat.

Around advice

About notification type, need to be analyzed separately is around advice, he is not like other types of notifications, around advice is the most powerful kind of notification,

Called around advice, as the name suggests, it allows you to write logic will be notified of all packaged target method.

In fact just before we write the previous meeting, what things dry after the meeting, for around advice, one method to get, because he surrounded the target method,

Equivalent to a notification method, while pre-prepared advice and after notification, the notification will surround is performed before and after the meeting, the meeting like logic.

 

 

From the original: https://i.cnblogs.com/EditPosts.aspx?opt=1

 

 

 

 

 

@Aspect@Componentpublic class LoggingAspect {
@Before("execution(* cn.ffcs.msa.springAop.six.CustomerBoImpl.addCustomer(..))")public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");System.out.println("hijacked : " + joinPoint.getSignature().getName());System.out.println("hijacked : " + joinPoint.getTarget());System.out.println("******");}
@After("execution(* cn.ffcs.msa.springAop.six.CustomerBoImpl.addCustomer(..))")public void logAfter(JoinPoint joinPoint) {System.out.println("******");System.out.println("logAfter() is running!");System.out.println("hijacked : " + joinPoint.getSignature().getName());System.out.println("******");
}@AfterReturning(pointcut="execution(* cn.ffcs.msa.springAop.six.CustomerBoImpl.addCustomerReturnValue(..))", returning= "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("logAfterReturning() is running!");System.out.println("hijacked : " + joinPoint.getSignature().getName());System.out.println("Method returned value is : " + result);System.out.println("******");
}@AfterThrowing(pointcut="execution(* cn.ffcs.msa.springAop.six.CustomerBoImpl.addCustomerThrowException(..))",throwing="error")public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
System.out.println("logAfterThrowing() is running!");System.out.println("hijacked : " + joinPoint.getSignature().getName());System.out.println("Exception : " + error);System.out.println("******");
}@Around("execution(* cn.ffcs.msa.springAop.six.CustomerBoImpl.addCustomerAround(..))")public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("logAround() is running!");System.out.println("hijacked method : " + joinPoint.getSignature().getName());System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));System.out.println("Around before is running!");joinPoint.proceed();System.out.println("Around after is running!");System.out.println("******");
}

Guess you like

Origin www.cnblogs.com/hejj-bk/p/11496505.html