Spring. Net learning notes 13--aop concept (basic) level 200

Source: Internet
Author: User

In the previous article, we briefly understoodAOPApplication scenarios, knowAOPProgramming importance. Let's take a look at this article.CodeTo start today's learning.

Review the code similar to the previous article:SecurityserviceClass ispass determines that the user name is"Admin"You have the permission to save the data.OrderserviceTo save the data class, implementIorderserviceInterface.

Code
Public   Class Securityservice
{
Public   Bool Ispass ( String Username)
{
Return Username =   " Admin " ;
}
}

Public InterfaceIorderservice
{
StringUsername {Get;Set;}

ObjectSave (ObjectID );
}

 

Implementation

Orderservice
Public   Class Orderservice: iorderservice
{
Public   String Username { Get ; Set ;}

Public ObjectSave (ObjectID)
{
Return "Save:" +Id. tostring ();
}
}

 

Aroundadvise
Public   Class Aroundadvise: imethodinterceptor
{
Public   Object Invoke (imethodinvocation Invocation)
{
Iorderservice target = (Iorderservice) Invocation. target;
Securityservice Manager =   New Securityservice ();
If (Manager. ispass (target. username ))
{
Return Invocation. Proceed ();
}
Else
{
Return   Null ;
}
}
}

 

Client:

Program
Class Program
{
Static   Void Main ( String [] ARGs)
{
Proxyfactory Factory =   New Proxyfactory ( New Orderservice () {Username =   " Admin " });

factory. addadvice ( New aroundadvise ());
iorderservice service = (iorderservice) factory. getproxy ();
Object result = service. save ( 1 );

Console. writeline (result );

Console. Readline ();
}
}

Output:Save: 1

 

Through the previous study, we know what isAOP-- Aspect-Oriented Programming. InAOP(Aspect-Oriented Programming), We compileProgramFirst, consider which methods in the program should be intercepted, and what business processing should be done after these methods are intercepted. These follow-up processes are called:Cross-cutting concerns. ByCross-cutting concernsThe following concepts are introduced:

Aspect(Section): RefersCross-cutting concernsIt is similar to a class, but the focus of the two is different. The class is the abstraction of Object Features, while the aspect is the abstraction of cross-cutting concerns. The section in the program isAroundadviseClass.

Joinpoint(Connection Point): The connection points refer to the intercepted points. In spring. net, connection points refer to methods, because spring. Net only supports connection points of the method type. In factJoinpoint(Connection PointIt can also be a field or a class constructor. The connection points in the program are intercepted, as shown in figureOrderserviceClass.

Pointcut(Entry Point): The so-called starting point refers toJoinpoint(Connection Point. The program does not use invocation. method to determine which methods to intercept, andPointcut(Entry Point)All methods are intercepted.

Advice(Notification): The notification is interceptedJoinpoint(Connection PointIn the future, notifications will be implemented. notifications can be divided into pre-notification, post-notification, exception notification, and surround notification.AroundadviseInheritanceAopalliance. Intercept. imethodinterceptorInterfaceIn the program, surround notifications are used.

Target(Target object): The target object of the proxy. The target object in the program isOrderserviceClass, we use

ProxyfactoryFactory =NewProxyfactory(NewOrderservice() {Username ="Admin"});

The Code confirms that the target object isOrderservice.

 

AOPProxy(AOPProxy):AOPThe object created after the framework applies the notification to the target object. The proxy object created by using the getproxy () method in the program.

Weave(Woven): RefersSection(Aspect) ApplyTarget object(Target) Object and causeProxy(ProxyThe process of object creation is called weaving. As the application in the program,OrderserviceClass does not have the ability to determine permissions, we will judge the function of permissions --SecurityserviceThe process of applying the ispass method of the class to the target object.

Introduction(Introduction): Without modifying the class code,Introduction(Introduction): You can dynamically add some methods or fields to the class at runtime. The program is not modifiedOrderserviceClass, but passes the permission judgment function at runtimeProxyfactoryThe addadvice method of is added dynamically.

These terms do not belong to spring. net, but belong to the wholeAOPProgramming. The so-calledAOPIn my understanding, it should be such a process. First, we need to define a plane. This plane is a class, and the method in it is the focus (also the notification ), or the method is the pre-notification, post-notification, exception notification, final notification, and surround notification to be executed when the target object method is executed. With regard to the aspect and notification, to apply the notification to the target object, you need to define the starting point of the notification. In other words, you need to intercept the methods. These intercepted methods are connection points, the so-called connection point is the method that is woven into the plane in the dynamic execution process (at least in spring.. net ). Therefore, in a dynamic process, the notification execution is a weaving process, and the objects that are woven into these notifications are the target objects.

 

Code download

 

 

Returned directory
 

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.