Spring AOP: my views

Source: Internet
Author: User

AOP is short for Aspect Oriented Programming, meaning: Aspect-Oriented Programming (also called Aspect-Oriented ), the pre-compilation method and runtime dynamic proxy can be used to dynamically and uniformly add functions to the program without modifying the source code. Also known as Aspect-Oriented: Aspect Oriented Programming (AOP) is a hot spot in software development and an important part of Spring framework. AOP can be used to isolate all parts of the business logic, thus reducing the Coupling Degree between each part of the business logic, improving the reusability of the program, and improving the development efficiency. Why? The plain explanation of AOP is: like the Highway Toll Station, I believe there will be no silly people building a toll station for every car. A toll station is enough. Don't worry, you are a god carriage, no license. This is the function of AOP. He wasn't part of the road, but I added it to drive without a license.

Let's take a look at the implementation of AOP in common Java programs:

[Java]
Public class UserDaoImp implements UserDao {
 
Private User user;
Public UserDaoImp (){}
Public UserDaoImp (User user)
{
This. user = user;
}
Public User getUser (){
Return user;
}

Public void delete (User user ){
System. out. println ("delete ....");
 
}
 
Public void select (User user ){
// TODO Auto-generated method stub
System. out. println ("select ....");
 
}
 
Public void update (User user ){
// TODO Auto-generated method stub
System. out. println ("update ....");
 
}
 
}

 

[Java]
Public class AopInterceptor implements InvocationHandler {
 
Private Object targetObject; // target Object
Public Object createObjectProxy (Object targetObject) // create a proxy Object
{
This.tar getObject = targetObject;
// Load the target class, the interface implemented by the target class, and the partition class
Return Proxy.newProxyInstance(this.tar getObject. getClass (). getClassLoader (), this.tar getObject. getClass (). getInterfaces (), this );
}
Public Object invoke (Object proxy, Method method, Object [] args)
Throws Throwable {
// Surround advice
System. out. println ("surround advice .....");
UserDaoImp userdaoimp((userdaoimp)this.tar getObject;
If (userDaoImp. getUser ()! = Null)
{

// Return the target Class Method
Try
{
// Front-end advice
System. out. println ("front advice .....");
Return method. invoke (targetObject, args );
// Post-advice

}

Catch (Exception e)
{
// Exception advice
System. out. println ("abnormal advice .....");
}
Finally
{
// The final advice
System. out. println ("final advice .....");
}

}
Return null;
}
 
}


Author: jzh440

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.