Introduction to AOP programming-Java

Source: Internet
Author: User

Aspect Oriented Programming (AOP) is a hot topic for Aspect-Oriented Programming. The main purpose of AOP is to extract the sections in the business processing process. It is faced with a certain step or stage in the processing process, in order to obtain the isolation effect of low coupling between different parts in the logical process. For example, the most common one is logging. For example, we now provide a service to query student information, but we want to record who has performed this query. Based on the traditional OOP implementation, StudentInfoService and StudentInfoServiceImpl are implemented. java), and in order to record, we are implementing the class (StudentInfoServiceImpl. java. In this case, what if we want to implement multiple services? Add these record processes to each implemented class. In this case, it will be a little complicated, and every implementation class is tightly coupled with the behavior of recording service logs, violating the object-oriented rules. So how can we separate the recorded service behavior from the business processing process? It seems that the Service for querying students is in progress, but the log records are used to record these behaviors. However, the Service for querying students does not know the existence of these record processes, this is where we want to discuss the purpose of AOP. The programming of AOP seems to be to isolate the functions proposed in a certain aspect from a batch of objects. This reduces coupling with a batch of objects and allows programming of a certain function.
Let's start with the Code. To achieve the above goal, we can use a dynamic Proxy class to intercept the behavior of an object and add the required functions. Java. lang. reflect. proxy class and java. lang. reflect. the InvocationHandler interface provides a solution for implementing the dynamic proxy class, but the solution implements some interfaces for the objects. If the purpose is a class, cglib provides us with another implementation solution. And so on.
I. Interface Implementation Scheme:
1) first write our business interface (StudentInfoService. java ):
Public interface StudentInfoService {
Void findInfo (String studentName );
}
And its implementation class (StudentInfoServiceImpl. java ):
Public class StudentInfoServiceImpl implements StudentInfoService {
Public void findInfo (String name ){
System. out. println ("your current name is:" + name );
}
}
2) Now we need a log function to execute and record the findInfo behavior before the findInfo behavior, so we must first intercept this behavior. In the actual execution process, a proxy class is used for us. Java provides a solution for implementing dynamic proxy classes:

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.