The Throwsadvice of Spring AOP

Source: Internet
Author: User
Tags aop define implement interface require throwable

The current project touches on some of the spring AOP parts, such as declarative transaction management. After understanding the implementation of AOP in spring, it is true that this new programming idea does provide us with a new way to solve the problem. In order to record the learning process, tidy up the study notes.







1. Several important concepts (see the online documentation for spring in detail)







· PointCut: A group of Jointpoint. In spring we can define those jointpoint by some regular expressions that make up a pointcut we need so that our advice can be compiled.



· Introduction:introduction can add properties and methods to the existing class without modifying the class, thereby increasing its state and action;



· Target: A class that satisfies the conditions defined by pointcut, we can use advice for this class. Most of spring's AOP is implemented through the mechanism of dynamic proxies, and this target is the object that is being represented;



· Proxy: In order to apply a Advice to another class, such as implementing around Advice, or adding additional code before and after a method executes, the actual implementation must be to execute a section of Advice code and then execute the target's method, Then execute a advice code, that is, when the client executes a class, the actual execution is an agent, and the proxy passes the call to that target.



· Weaving (weaving): With target and advice, at what time will these two modules be woven together? The options that can be chosen include compile time (so we need a special compiler), loading the class (so we need a special ClassLoader) and running (so that AOP can easily create an agent dynamically so that calls are passed to the target class by the broker).







2. Throws Advice







There is a requirement in the current project that, for some processing processes to throw some exceptions when running, they need to be logged, stored in a database, or emailed to the developer. Let's not say what's going to happen with TDD, let's see how it's going to work.







As described in the above concept, we should focus on three concepts: Target,advice and proxy.







Implementation of 2.1 target







Target is the business process class mentioned above, we write code in accordance with normal development, there is no special requirements. Such as:







public interface Ibizprocess



{



void Doonething ();



void Doanotherthing ();



}







public class Bizprocessimpl implements Ibizprocess



{



public void doonething ()



{



}







public void doanotherthing ()



{



throw new RuntimeException ("Boom");



}



}







2.2 Advice







In order to implement the advice when the business process throws an exception, we need to define a advice class to implement the Throwsadvice interface. There is no method defined in this interface, we require our class to implement afterthrows this method, as follows:







public void Afterthrows ([method method,] [object args,] [object target,] throwable throwable);







The previous three parameters of this method are optional. We define multiple versions of this method in the same class, such as:







public void afterthrowing (MyException1 ex) {}



public void afterthrowing (MyException2 ex) {}







The specific method is invoked to judge by the specific exception, which can be easily recognized by AOP automatically.







2.3 Proxy (proxy)







A simple implementation in spring is to use its org.springframework.aop.framework.ProxyFactoryBean. This bean contains a number of attributes, three of which we need to set: Target,proxyinterfaces and Interceptornames, as follows:







<bean id= "Bizonetarget" class= "Com.company.biz.impl.BizProcessImpl"/>



<bean id= "Throwsadvice" class= "Com.company.advice.MyThrowsAdvice"/>



<bean id= "BizOne" class= "Org.springframework.aop.framework.ProxyFactoryBean" >



<property name= "target" ><ref bean= "Bizonetargte"/></property>



<property name= "Proxyinterfaces" >



<value>com.company.biz.IBizProcessImpl</value>



</property>



<property name= "Interceptornames" >



<list>



<value>throwsAdvice</value>



</list>



</property>



</bean>







With the above configuration, spring weaves target and advice together. It should be explained that both proxyinterfaces and interceptornames can be multiple, and if they are multiple, they need to be defined with a list. The order of Interceptornames determines the order in which these advice are executed.











3. Simplify configuration







As you can see from the example above, if you have multiple Bizprocess objects that require proxies, and we configure one agent for each bean in the spring configuration, then the maintenance of the configuration file becomes cumbersome. For this reason, Spring provides a convenient way to solve this problem, such as Beannameautoproxycreator, Defaultadviceautoproxycreator, and metadata autoproxying. We used Beannameautoproxycreator, because he was more intuitive and simple.











The configuration is as follows:







<bean id= "Beannameautoproxycreator" class= " Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator ">



<property name= ' Beannames ' >



<list>



<value>*Service</value>



</list>



</property>



<property name= "Interceptornames" >



<value>throwsAdvice</value>



</property>



</bean>







From this we can see that all the beans that end with the service are automatically created by spring to create the proxy, which enables the advice to be woven.




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.