Benefits of using AOP
Original code writing
Since we want to use code for demonstration, there must be an example. Here my example is:
There is an interface Dao that has three methods: insert, delete, and update. Before and after the insert and update operations are called, print the number of milliseconds before the call and the number of milliseconds after the call.
First, define a Dao interface:
public interface Dao { public void insert(); public void delete(); public void update();}
Then define an implementation class DaoImpl:
public class DaoImpl implements Dao { @Override public void insert() { System.out.println("DaoImpl.insert()"); } @Override public void delete() { System.out.println("DaoImpl.delete()"); } @Override public void update() { System.out.println("DaoImpl.update()"); }}
In the original statement, I want to print the time before and after the insert () and update () methods, only one new class package layer can be defined () process the methods before and after the update () method
The new class is named ServiceImpl and its implementation is:
Public class ServiceImpl {private Dao dao = new DaoImpl (); public void insert () {System. out. println ("insert () method start time:" + System. currentTimeMillis (); dao. insert (); System. out. println ("insert () method end time:" + System. currentTimeMillis ();} public void delete () {dao. delete ();} public void update () {System. out. println ("update () method start time:" + System. currentTimeMillis (); dao. update (); System. out. println ("update () method end time:" + System. currentTimeMillis ());}}
This is the most primitive writing method, and the disadvantages of this writing method are also clear at a glance:
The logic of the output time before and after method calling cannot be reused. If you want to add this logic elsewhere, you have to write it again.
If Dao has other implementation classes, a new class must be added to wrap the implementation class, which will cause the number of classes to expand.
Use AOP
Finally, let's take a look at the method of using AOP. First, define a time processing class. I name it TimeHandler:
Public class TimeHandler {public void printTime (ProceedingJoinPoint pjp) {Signature signature = pjp. getSignature (); if (signature instanceof MethodSignature) {MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature. getMethod (); System. out. println (method. getName () + "() method start time:" + System. currentTimeMillis (); try {pjp. proceed (); System. out. println (method. getName () + "() method end time:" + System. currentTimeMillis ();} catch (Throwable e) {e. printStackTrace ();}}}}
The cropping method printTime itself does not need to define any parameters, but in some scenarios, You need to obtain information such as the class and signature of the called method. At this time, you can
JointPoint is defined in the printTime method. Spring automatically injects parameters. You can use JoinPoint to obtain information about the classes and signatures of the called methods.
. Because I use aop: around here, we need to ensure the call of the method so that we can output the time before and after the method call. Therefore, we cannot directly use JoinPoint,
Because JoinPoint cannot guarantee method calling. In this case, you can use ProceedingJoinPoint. The proceed () method of ProceedingPointPoint can be guaranteed.
Method call, but note that ProceedingJoinPoint can only be used with aop: around. In other words, if aop. xml is configured with aop: before,
Then, if the printTime method parameter is ProceedingJoinPoint, an error will be reported when the Spring container starts.
Next, let's take a look at the configuration of ApplicationContext. xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>
<Bean id = "daoImpl" class = "org. xrq. spring. action. aop. DaoImpl"/>
<Bean id = "timeHandler" class = "org. xrq. spring. action. aop. TimeHandler"/>
<Aop: config>
<Aop: pointcut id = "addAllMethod" expression = "execution (* org. xrq. spring. action. aop. Dao. * (...)"/>
<Aop: aspect id = "time" ref = "timeHandler">
<Aop: before method = "printTime" pointcut-ref = "addAllMethod"/>
<Aop: after method = "printTime" pointcut-ref = "addAllMethod"/>
</Aop: aspect>
</Aop: config>
</Beans>
The test code is simple:
Public class AopTest {@ Test @ SuppressWarnings ("resource") public void testAop () {ApplicationContext ac = new ClassPathXmlApplicationContext ("spring/aop. xml "); Dao dao = (Dao) ac. getBean ("daoImpl"); dao. insert (); System. out. println ("---------- split line ----------"); dao. delete (); System. out. println ("---------- split line ----------"); dao. update ();}}
Here I will summarize several advantages of using AOP:
The aspect content can be reused. For example, the TimeHandler printTime method can be used to print the time before the method execution and the time after the method execution in any place.
Avoid using Proxy and CGLIB to generate Proxy. All the work in this aspect is implemented by the Framework. developers can focus on the aspect content.
There is no coupling between the code and the code. If the interception method changes, modify the configuration file.