Spring: Use Spring AOP to separate log input and method. spring AOP

Source: Internet
Author: User

Spring: Use Spring AOP to separate log input and method. spring AOP

Log output is a common function. The traditional method is to write the output statement inside the method body. When calling this method, use the input statement output information to record the execution of the method!

1. First write a common class:

Package com. importnew; public class Common {public void execute (String username, String password) {System. out. println ("------------------ execute () method ----------------");}}

 

2. Write a partition class for adding the legality checksum and log:

Package com. importnew; import org. aspectj. lang. JoinPoint;
Public class Check {public void checkValidity () {System. out. println ("------------------ Verify validity ----------------");} public void addLog (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 method name }}

 

3. Configure AOP in XML format: (pay attention to the content of the Red Flag)

<?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: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-context.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">                   <bean id="common" class="com.importnew.Common"/>            <bean id="check" class="com.importnew.Check"/>             <aop:config>        <aop:aspect id="myAop" ref="check">          <aop:pointcut id="target" expression="execution(* com.importnew.Common.execute(..))"/>          <aop:before method="checkValidity" pointcut-ref="target"/>          <aop:after method="addLog" pointcut-ref="target"/>        </aop:aspect>      </aop:config>     </beans>

 


Note: aop pointcut expression (*)
Execution (method Modifier + complete class name returned value + method name (method parameter ))
For example:
A. execution (public void * (..): all methods whose return values are public void will be intercepted.
B. execution (public void day6.com. beans. PersonService. * (...): all methods that return values of public void in day6.com. beans. PersonService will be intercepted.
C. execution (public void day6.com. beans. personService. save (java. lang. string ...)): day6.com. beans. the save method of the first parameter type in the PersonService class is String.
D. execution (public void save (...): indicates that the save method in all classes will be intercepted.
E. execution (public void day6.com. service... * (...): indicates that classes in the day6.com. service package and classes in the sub-package will be intercepted by all public void methods.
F, execution (public! Void day6.com. service... * (...): indicates the classes in the day6.com. service package and the classes in the sub-package. All methods that are public and not return the void type will be intercepted.

4. Write a test:

package test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.importnew.Common;public class Client {    public static void main(String[] args) {        BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");         Common c=(Common) factory.getBean("common");         c.execute("fuckyou","fuckme");    }}

 

Note:

Three packages need to be added: spring-aop.jar, aspectjrt. jar, aspectjweaver. jar; otherwise, an error is reported.

Output result:

------------------ Verify validity when execute () method ------------------------------------ add log -------------- fuckyoufuckme ===== checkSecurity = execute

 

 

/// End

Related Article

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.