Logging out of a method is a common feature. The traditional way is to write the output statement inside the method body, when the method is called, the input statement output information to record the execution of the method!
1. Write a generic class first:
Package com.importnew; Public class Common { publicvoid execute (String username,string password) { System.out.println ("------------------Execute Execute () method----------------");} }
2. Write a section class for the legality checksum log to add:
Packagecom.importnew;ImportOrg.aspectj.lang.JoinPoint;
Public classCheck { Public voidcheckvalidity () {System.out.println ("------------------Verify legality----------------"); } Public voidAddlog (Joinpoint j) {System.out.println ("------------------Add log----------------"); Object obj[]=J.getargs (); for(Object o:obj) {System.out.println (o); } System.out.println ("========checksecurity==" +j.getsignature (). GetName ());//This is the name of the Get method }}
3. Configure AOP, using XML: (Note the contents of the red flag)
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"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/context http://www.springframework.org/schema/context/spring-conte Xt.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <BeanID= "Common"class= "Com.importnew.Common"/> <BeanID= "Check"class= "Com.importnew.Check"/> <Aop:config> <Aop:aspectID= "MYAOP"ref= "Check"> <Aop:pointcutID= "Target"expression= "Execution (* com.importnew.Common.execute (..))"/> <Aop:beforeMethod= "Checkvalidity"Pointcut-ref= "Target"/> <Aop:afterMethod= "Addlog"Pointcut-ref= "Target"/> </Aop:aspect> </Aop:config> </Beans>
Note: An AOP pointcut expression (*)
Execution (method modifier + return value Full class name + Method name (method parameter))
For example:
A, Execution (public void * (..)) : All methods that return a public void will be intercepted
B, Execution (public void day6.com.beans.personservice.* (..)) : Indicates that all methods that return values of public void under Day6.com.beans.PersonService are intercepted
C, Execution (public void Day6.com.beans.PersonService.save (java.lang.String ...)) : Indicates that the first parameter type in the Day6.com.beans.PersonService class is a string and the Save method is intercepted
D, Execution (public void Save (..)) : Indicates that the Save method in all classes is blocked to
E, Execution (public void Day6.com.service). *(..)) : The class that represents the Day6.com.service package and the class of the child package all public void methods will be intercepted to
F, Execution (public!void day6.com.service. *(..)) : The class that represents the Day6.com.service package and the class of the child package all public methods that are not void return types will be intercepted to
4. Finally write a test:
Packagetest;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.importnew.Common; Public classClient { Public Static voidMain (string[] args) {Beanfactory factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Common C= (Common) factory.getbean ("Common"); C.execute ("Fuckyou", "FuckMe"); }}
Attention:
Need to add three packages: Spring-aop.jar, Aspectjrt.jar, Aspectjweaver.jar, otherwise it will be an error.
Output Result:
------------------Verify legality----------------------------------execution Execute () method----------------------------------add log----------------fuckyoufuckme========checksecurity== Execute
End
Spring: Using spring AOP to separate log input from the method