Aop Method-level interception

Source: Internet
Author: User
Tags aop regular expression throwable

says that AOP has to mention several concepts:

Facets: Some of our own business methods.

Notification: The operation to be started when intercepting.

Pointcut: A specific intercept of a business point.

This may still be a bit abstract, for example, here is a paper paste of the Multipatch.

Each polygon is a business method, and we can penetrate into the interior by piercing each of the faces, which is a facet.

It makes a sound when pierced, which is a notice .

and specifically from which side of the penetration, this is a point of choice.

That said, you should be able to understand a little bit.

So here's a simple example:

In order to clarify the relationship, first put a related class diagram:

First define an interface

1 public interface IService {
2 public  void Withaop ();
3 Public  void Withoutaop ();
4}

With an interface, of course, an implementation class is required:

1 public class Testaop implements IService {
 2  private String name;
 3 Public  void Withaop () {
 4   System.out.println ("with AOP name:" +name);
 5  }
 6 public  void Withoutaop () {
 7   System.out.println ("Without AOP Name:" +name);
 8  }
 9 public  String GetName () {
ten   return name;
One  }
public  void SetName (String name) {   this.name = name;
15}

This implementation class implements the two methods of interface definition, below which we define several interception methods, which are different depending on the location or timing of the interception.

There are usually methods to intercept before, methods to intercept, and abnormal interception. By writing their own business processes in these interception, specific requirements can be achieved.

Method before interception, need to implement Methodbeforeadvice interface, and fill in the Before method. This way, when a method is intercepted, the before () method is executed before the method executes.

1 public class Beforeaopinterceptor implements methodbeforeadvice{
2 public  Void Before (method method, Object [] args, Object instance)
3    throws Throwable {
4   System.out.println ("before ()" +method.getname () );
5  }
6}

Similarly, the method is intercepted after the same. The Afterreturningadvice interface needs to be implemented.

1 public class Afteraopinterceptor implements afterreturningadvice{
2 public  void afterreturning (Object value, method, object[] args,
3    Object instance) throws Throwable {
4   System.out.println ("after ()" + Method.getname ());
5  }
6}

and exception interception.

1 public class Throwsaopinterceptor implements throwsadvice{ 
2 public  void afterthrowing (Method method,object [] Args,object instance,accountexception ex) throws throwable{
3   System.out.println ("after ()" + Method.getname () + "throws Exception:" +ex);
4  }
5 public  void Afterthrowing (NullPointerException ex) throws throwable{
6   System.out.println ("Throws Exception:" +ex);
7  }
8}

The next step is to configure the spring configuration file to associate the Interceptor with the tangent method.

Refer to the diagram above to see the hierarchical relationships in the configuration file.

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 3 XM Lns= "Http://www.springframework.org/schema/beans" 4 xsi:schemalocation= "http://www.springframework.org/schema/   Beans 5 Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> 6 <!--by name and 7 <!--8 <bean id= "Before" class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > 9 <property Nam e= "Advice" > <bean class= "Com.test.pointcut.beforeAOP" ></bean> </property> 12 & Lt;property name= "Mappedname" value= "WITHOUTAOP" ></property> </bean>--<!--by regular expression -<bean id= "before" class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor" > &LT;PR Operty name= "Advice" > <bean class= "com.test.pointcut.BeforeAOPInterceptor" ></bean> </p roperty> <property NamE= "Patterns" > <list> <value>.*out.*</value> </list> </proper ty> </bean> <bean id= "after" class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor" &G 
T <p

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.