Two ways of writing aspect and advisor for Spring AOP

Source: Internet
Author: User

This article transferred from: https://www.cnblogs.com/leiOOlei/p/3709607.html

First look at an example, as follows

Interface code:

 Package Com.lei.demo.aop.schema;  Public Interface Ihello {    publicvoid  SayHello ();}

Interface implementation:

 Package Com.lei.demo.aop.schema;  Public class Implements Ihello {    publicvoid  SayHello () {        System.out.println ("-----Hello World !-----");    }}

The next step is to implement AOP, which is to cut into notifications when calling the SayHello method.

1. The first method configures Aop:pointcut and Aop:aspect in Aop:config

Define a facet support class Helloaspect.java

 PackageCom.lei.demo.aop.schema;/** Slice support class*/ Public classHelloaspect {//front-facing notifications     Public voidBeforeadvice () {System.out.println ("===========before Advice"); }        //Post Final notification     Public voidAfterfinallyadvice () {System.out.println ("===========after Finally Advice"); }}

XML configuration: Spring-aop-schema.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/ Aop/spring-aop-3.2.xsd ">               <!--Target Class -    <BeanID= "HelloService"class= "Com.lei.demo.aop.schema.HelloService" />        <BeanID= "Helloaspect"class= "Com.lei.demo.aop.schema.HelloAspect" />            <!--Configure Facets -    <!--Aop:advisor, there is Order, must be placed after Aop:pointcut -    <Aop:config>        <Aop:pointcutID= "Hellopointcut"expression= "Execution (* com.lei.demo.aop.schema). *.*(..))" />                <Aop:aspectref= "Helloaspect">            <!-The following two methods are used to define pointcuts Pointcut-ref and pointcut-->            <Aop:beforePointcut-ref= "Hellopointcut"Method= "Beforeadvice" />            <Aop:afterpointcut= "Execution (* com.lei.demo.aop.schema). *.*(..))"Method= "Afterfinallyadvice" />        </Aop:aspect>                    </Aop:config></Beans>

In the above configuration method="Beforeadvice",method="Afterfinallyadvice" corresponding to the method in Helloaspect

Test, App.java

 PackageCom.lei.demo.aop.schema;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApp {Private StaticApplicationContext CTX;  Public Static voidMain (string[] args) {CTX=NewClasspathxmlapplicationcontext ("Spring-aop-schema.xml"); Ihello HelloService= Ctx.getbean ("HelloService", Ihello.class);    Helloservice.sayhello (); }}

Run App.java with the following results:

===========before Advice

-----Hello World!-----

===========after finally advice

2. The second method configures Aop:pointcut and Aop:advisor in Aop:config

Implement the Org.aopalliance.intercept.MethodInvocation interface,

Helloaroundadvice.java as follows:

 PackageCom.lei.demo.aop.schema;ImportJava.lang.reflect.Method;ImportOrg.aopalliance.intercept.MethodInterceptor;Importorg.aopalliance.intercept.MethodInvocation; Public classHelloaroundadviceImplementsMethodinterceptor { PublicObject Invoke (Methodinvocation arg0)throwsthrowable {System.out.println ("++++++before Advice");        Arg0.proceed (); System.out.println ("++++++after Advice"); return NULL; }}

XML configuration: Spring-aop-schema.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/ Aop/spring-aop-3.2.xsd ">               <!--Target Class -    <BeanID= "HelloService"class= "Com.lei.demo.aop.schema.HelloService" />        <BeanID= "Helloarroundadvice"class= "Com.lei.demo.aop.schema.HelloAroundAdvice" />        <!--Configure Facets -    <Aop:config>        <Aop:pointcutID= "Hellopointcut"expression= "Execution (* com.lei.demo.aop.schema). *.*(..))" />        <Aop:advisorAdvice-ref= "Helloarroundadvice"Pointcut-ref= "Hellopointcut"/>            </Aop:config></Beans>

Still running App.java, the results are as follows:

++++++before Advice

-----Hello World!-----

++++++after Advice

Two ways of writing aspect and advisor for Spring AOP

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.