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