Original link: http://www.orlion.ga/205/
I. Introduction to AOP
1. AOP Concept
Reference article: HTTP://WWW.ORLION.ML/57
2, the generation of AOP
For the following methods:
public class Userdaoimpl implements userdao{public void Saveuser (user user) {dosaveuser (); } }
In the Saveuser method, before and after the save user to record the current time in order to saveuser how much time spent, methods there are many ways, the most intuitive way to write in Dosaveuser () and before and after the code to take out the current time:
public class Userdaoimpl implements userdao{public void Saveuser (user user) {int beginTime = GetCurrentTime (); Dosaveuser (); int endTime = GetCurrentTime (); } }
Another way is to rewrite a class that inherits from Userdaoimpl and then overrides the Saveuser method, as follows:
public class USERDAOIMPL2 extends userdaoimpl{@Overridepublic void saveuser (user user) {int beginTime = Getcurrentt IME (); Super.saveuser (); int endTime = GetCurrentTime ();} }
This method of coupling is too strong, once the parent class changes the subclass will also change, with caution with inheritance
Another way is to add code when calling the Saveuser () method:
public class userservice{public void Saveuser (user user) {Userdaoimpl Userdao = new Userdaoimpl (); int begin Time = GetCurrentTime (); Userdao.saveuser (); int endTime = GetCurrentTime ();} }
Now, if you let us put all of the database CRUD operations on the project together with the code to get time, obviously the workload is too large, this time with the dynamic Agent : (can refer to http://www.orlion.ml/207/)
Userdaoimpl.java
Package Ml.orlion.dao.impl; Import Ml.orlion.dao.UserDAO; Import Ml.orlion.model.User; public class Userdaoimpl implements userdao{public void Saveuser (user user) {System.out.println ("Save Usering") ; } }
timeinterceptor.java
package ml.orlion.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class TimeInterceptor implements invocationhandler{ private object target;// The object being proxied public object gettarget () { return target; } public void Settarget (Object target) { this.target = target; } public void beforemethod (method m) { System.out.println (M.getname () + "Begin start"); } @Override     PUBLIC&NBSp;object invoke (Object proxy, method m, object[] args) throws Throwable { this.beforemethod (m);// insertion method m.invoke (Target, args); return null; } }
Test
public static void Testproxy () {//first produces a proxied object Userdao Userdao = new Userdaoimpl ();// The next step is to hand over the proxy object to Invocationhandler, Timeinterceptortimeinterceptor ti = new Timeinterceptor ();//Set the proxy object Ti.settarget ( Userdao);//generates a proxy Userdao userproxy = (Userdao) proxy.newproxyinstance (UserDAO.class.getClassLoader () According to the proxy object, new Class[]{userdao.class}, TI); Userproxy.saveuser (new User ()); }
Run can see the console print: Saveuserbegin start save usering
Ii. using Spring AOP
Userdao.java:
Package Ml.orlion.dao;import Ml.orlion.model.user;public interface Userdao {public void Saveuser (user user);
Userdaoimpl.java:
Package Ml.orlion.dao.impl;import Ml.orlion.dao.userdao;import Ml.orlion.model.user;public class UserDAOImpl Implements Userdao{public void Saveuser (user user) {System.out.println ("save Usering");}}
Userservice.java
Package Ml.orlion.service;import Ml.orlion.dao.userdao;import Ml.orlion.dao.impl.userdaoimpl;import Ml.orlion.model.user;public class UserService {private Userdao Userdao = new Userdaoimpl ();p ublic Userdao GetUserDao () {R Eturn Userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} public void Saveuser (user user) {this.userDAO.saveUser (user);}}
Loginterceptor.java
Package Ml.orlion.aop;public class Loginterceptor {public void before () {System.out.println ("before");}}
Beans.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" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsd http:// www.springframework.org/schema/aop http:// Www.springframework.org/schema/aop/spring-aop.xsd "><bean id=" Loginterceptor " class=" Ml.orlion.aop.LogInterceptor "></bean> <bean id=" Userdao " class=" Ml.orlion.dao.impl.UserDAOImpl "> </bean> <Bean id= "UserService" class= "Ml.orlion.service.UserService" > <property name= " Userdao " ref=" Userdao "/>&NBSP;&NBSP;</BEAN>&NBSP;&NBSP;<AOP:CONFIG>&NBSP;&NBSP;<AOP: pointcut expression= "Execution (Public * ml.orlion.service. *.add (..)) " id= "Servicepointcut" /> <aop:aspect id= "LogAspect" ref= " Loginterceptor "> <aop:before method=" before " pointcut-ref=" ServicePointcut "/> </aop:aspect> </aop:config></beans>
Test:
Beanfactory appContext = new Classpathxmlapplicationcontext ("Beans.xml"); UserService UserService = (userservice) appcontext.getbean ("UserService"); Userservice.saveuser (new User ());
Spring (iii) AOP oriented tangent programming