"frame" [SPRING]AOP intercept-use Tangent point: aspectjexpressionpointcut-tangent Point language

Source: Internet
Author: User
Tags throwable

Reprint Please specify source: http://blog.csdn.net/qq_26525215

This article is from the "university trip _ remember the blog"

The advantage of using Aspectjexpressionpointcut to realize pointcuts is that it is possible to use the tangent language to more accurately represent which method to intercept when setting a tangent point than jdkregexpmethodpointcut.

Can be accurate to return parameters, parameter types, method names.

of course, You can also blur the Match.
This is demonstrated in a pure Java manner and in the method of configuring Xml.
The package you need is not explained, if you do not move, please refer to the Previous.

first, prepare the Prototype object Person.

Package cn.hncu.spring3x.aop.aspectj; public classperson { public int Run() {System. out. println ("i'm at Run ...");return 0; } public void Run(intI) {System. out. println ("i'm in run...<."+i+">"); } public void say() {System. out. println ("i'm in say ..."); } public void Sayhi(String Name) {System. out. println ("Hi,"+name+"hello,"); } public int say(String name,intI) {System. out. println (name+"----"+ i);return 0; }}

then, There are two ways to intercept this Object.

Pure Java Method implementation

4 steps:
1, Declare the agent Factory.
2. Set the Pointcut
3. Set up Notifications
4. Add facets to the factory
Remember: tangent point = tangent + notification

Aspectjdemo
Package CN. Hncu. Xmlimpl. AspectJ;import org. Aopalliance. AOP. Advice;import org. Aopalliance. Intercept. Methodinterceptor;import org. Aopalliance. Intercept. Methodinvocation;import org. JUnit. Test;import org. Springframework. AOP. Advisor;import org. Springframework. AOP. AspectJ. Aspectjexpressionpointcut;import org. Springframework. AOP. Framework. Proxyfactorybean;import org. Springframework. AOP. support. Defaultpointcutadvisor;public class Aspectjdemo {@Test public void demo () {proxyfactorybean factory = new Proxyfactorybean ();Factory. Settarget(new person ());Declares a aspectj tangent aspectjexpressionpointcut cut = new Aspectjexpressionpointcut ();Set the method to intercept-write the cut in the tangent language. SetExpression("execution (int cn.hncu.xmlImpl.aspectj.Person.run ())");//intercept: NULL parameter returns the Run method with the value intAdvice Advice = new Methodinterceptor () {@Override public Object invoke (methodinvocation invocation ) throws Throwable {System. out. println("intercept before release ...");Object obj = invocation. Proceed();//releaseSystem. out. println("intercept after release ...");return obj;}        };Tangent point = pointcut + Notify Advisor Advisor = new Defaultpointcutadvisor (cut,advice);Factory. Addadvisor(advisor);Person P = (person) Factory. GetObject();P. Run();P. Run(Ten);P. Say();P. Sayhi("Jack");P. Say("Tom",666);}}
Operation Result:

Tangent language:

The Aspectjexpressionpointcut object is called In:
When setexpression, the parameter of this method is to use the tangent language.

Tangent language Format:

execution ( 返回类型 方法路径.方法名(参数) )
Example:
//declare a aspectj tangent pointAspectjexpressionpointcut cut =NewAspectjexpressionpointcut (); Cut.setexpression ("execution (int cn.hncu.xmlImpl.aspectj.Person.run ())");//intercept: NULL parameter returns the Run method with the value intCut.setexpression ("execution (void cn.hncu.xmlimpl.aspectj.person.* ())");//intercept: Any method of NULL parameter null return valueCut.setexpression ("execution (void cn.hncu.xmlimpl.aspectj.person.* (String))");//intercept: only 1 string type parameters, null return value of any methodCut.setexpression ("execution (void cn.hncu.xmlimpl.aspectj.person.* (*))");//intercept: There are 1 parameters (type unlimited), Any method of NULL return valueCut.setexpression ("execution (void cn.hncu.xmlimpl.aspectj.person.* (*,*))");//intercept: There are 2 parameters (type unlimited), Any method of NULL return valueCut.setexpression ("execution (void cn.hncu.xmlimpl.aspectj.person.* (..))");//intercept: arbitrary (number and Type) parameters, any method of NULL return valueCut.setexpression ("execution (int cn.hncu.xmlimpl.aspectj.person.* (*,..))");//intercept: There are at least 1 parameters (type unlimited), and the return value type is any method of intCut.setexpression ("execution (* cn.hncu.xmlimpl.aspectj.person.* (*,..))");//intercept: at least 1 parameters (type unlimited), return value type arbitrary methodCut.setexpression ("execution (* cn.hncu). *son.* (*,..)) ");//intercept: cn.hncu, class name ends with "son", at least 1 parameters (type unlimited), return value type arbitrary method

The parameters inside are matched to the regular Expression.

“.” Represents any character except \ r \ N.
"*" stands for 0 or More.

Because the tangent language cannot define multiple return values that are specified, for example:

If you need to intercept the void and int return value methods, you can resolve by defining 2 Pointcuts.

XML configuration AOP Intercept Aroundadvice
 packagecn.hncu.xmlImpl.aspectj;Importorg.aopalliance.intercept.MethodInterceptor;Importorg.aopalliance.intercept.MethodInvocation; public  class aroundadvice implements methodinterceptor{    @Override     publicObjectInvoke(methodinvocation Invocation)throwsThrowable {System.out.println ("front Intercept ...."); Object resobj = Invocation.proceed ();//releaseSystem.out.println ("behind The Intercept ...");returnresobj; }}
Configuration file
<?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"        xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.3.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/co Ntext/spring-context-4.3.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema /tx/spring-tx-4.3.xsd ">    <!--auto Agent --    <Bean class=" Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator "></Bean>    <bean id="p" class="cn.hncu.xmlImpl.aspectj.Person"></Bean>    <!--facets = tangency + notifications (to write pointcuts and notifications as internal beans)--    <bean id="cut" class=" Org.springframework.aop.aspectj.AspectJExpressionPointcut ">        <!--interception: cn.hncu, class name ends with "son", at least 1 parameters (type unlimited), return value type any method--      < property name="expression" value="execution (* cn.hncu). *son.* (*,..)) " ></Property >        </Bean>    <bean id= "advisor" class=" Org.springframework.aop.support.DefaultPointcutAdvisor ">       < property name="pointcut" ref="cut"></Property >       < property name="advice">            <bean id= "advice" class=" Cn.hncu.xmlImpl.aspectj.AroundAdvice "></Bean>       </Property >    </Bean></Beans>

This can also be configured in the Middle:

<!--auto Agent --    <Bean class=" Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator "></Bean>    <bean id="p" class="cn.hncu.xmlImpl.aspectj.Person" ></Bean>    <!--facets = tangency + notification (※※ is configured for Tangent-point language)--    <bean id= "advisor" class=" Org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor ">       < property name="expression" value="execution (* cn.hncu). *son.* (*,..)) " ></Property >       < property name="advice">            <bean id="advice" class="cn.hncu.xmlImpl.aspectj.AroundAdvice" ></Bean>       </Property >    </Bean>
Test Class:
Package CN. Hncu. Xmlimpl. AspectJ;import org. JUnit. Test;import org. Springframework. Context. ApplicationContext;import org. Springframework. Context. support. Classpathxmlapplicationcontext;public class Aspectjxmldemo {@Test public void demo1 () {applicationcontext ctx = new Classpathxmlapplication Context ("cn/hncu/xmlimpl/aspectj/aspectj.xml");Person p = CTX. Getbean(person. Class);P. Run();P. Run(Ten);P. Say();P. Sayhi("Jack");P. Say("Tom",666);}}
Test results

In this example: the XML configuration is compared to the pure Java approach, which is to create objects from the Java code new objects, through the XML Configuration.

In the case of a development project, with the spring framework, some of our xml-injected objects only need to rely on XML files.
Reliance on XML is not called dependency, which is independence.

This article is written by [understand the memory], All rights reserved.

Reprint Please specify source: http://blog.csdn.net/qq_26525215

This article is from the "university trip _ remember the blog"

"frame" [SPRING]AOP intercept-use Tangent point: aspectjexpressionpointcut-tangent Point language

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.