Spring-aop use Method (ii)

Source: Internet
Author: User
Tags aop stub throwable



This article describes how SPRING-AOP uses AOP using the way two, spring configuration files.


The Spring-applicationcontext.xml section code is formulated as follows:


  1. <!--component Scan--

  2. <context:component-scan base-package= "Com.lw.rpc"

  3. Use-default-filters= "false" >

  4. <context:include-filter type= "Annotation"

  5. expression= "Org.springframework.stereotype.Repository"/>

  6. <context:include-filter type= "Annotation"

  7. expression= "Org.springframework.stereotype.Service"/>

  8. </context:component-scan>


  9. <!--notice Class--

  10. <bean id= "ASPECTJADVICEAOP" class= "Com.lw.rpc.test.AspectJAdviceAop"/>

  11. <aop:config>

  12. <aop:aspect id= "Useaapect" ref= "ASPECTJADVICEAOP" >

  13. <!--configuring Pointcuts--and

  14. <aop:pointcut expression= "Execution (* com.lw.rpc. *.*(..))" Id= "Point_cut"/>

  15. <!--front-facing notifications-

  16. <aop:before method= "Beforeadvice" pointcut-ref= "Point_cut"/>

  17. <!--<aop:pointcut id= "Except_add" expression= "Execution (* com.lw.rpc). *.*(..)))" />--

  18. <!--post Notification returning specifies the return parameter, and if you do not set the return parameter, you can try it yourself and see what effect it is.

  19. <!--<aop:after-returning method= "Afteradvice" pointcut-ref= "point_cut" returning= "result"/>-

  20. <!--surround-

  21. <!--<aop:around method= "Aroundadvice" pointcut-ref= "Point_cut"/>--

  22. <!--throw-in

  23. <!--<aop:after-throwing method= "Afterthrow" pointcut-ref= "Point_cut" throwing= "E"/>--

  24. </aop:aspect>

  25. </aop:config>



The notification class, like the following, implements the four-way AOP interception in this class.


Package com.lw.rpc.test;


Import Org.aspectj.lang.JoinPoint;

Import Org.aspectj.lang.ProceedingJoinPoint;


public class Aspectjadviceaop {


/**

* Front-facing notification

*

* @param JP

*/

public void Beforeadvice (Joinpoint JP) {

System.out.println ("+++++++++++++++ before entering the connection point");


System.out.println (Jp.gettarget (). GetClass () + "object is using this");


System.out.println (Jp.getargs () [0] + "test");


System.out.println ("Enter to Connection point method");

}


/**

* Post Notification

*

* @param JP

*/

public void Afteradvice (Joinpoint JP, String result) {

System.out.println ("+++++++++++++++" after completion of the "Connection point method Execution");


System.out.println (Jp.gettarget (). GetClass () + "object is using this");


System.out.println (Jp.getsignature (). GetName () + "method");


SYSTEM.OUT.PRINTLN ("Result:" + result);

}


/**

* Surround Notifications

*

* @param JP

* @param result

* @throws Throwable

*/

public void Aroundadvice (Proceedingjoinpoint pjp) throws Throwable {

System.out.println ("Enter to surround notice +++++++++++++++");


Parameters of the Calling method

object[] args = Pjp.getargs ();

Method name to invoke

String method = Pjp.getsignature (). GetName ();

Get target Object

Object target = Pjp.gettarget ();


Execution of the return value of the method: calling the proceed () method triggers the Pointcut method execution

Object result = Pjp.proceed ();


System.out.println ("Call method End: Execute after!") \ n ");


System.out.println ("Output:" + args[0] + ";" + method + ";" + target + ";" + result + "\ n");

}

/**

* Exception Notification

*

* @param JP

* @param E

*/

public void Afterthrow (Joinpoint JP, Throwable e) {

SYSTEM.OUT.PRINTLN ("exception notification");

}


}


Target class, which target is to be targeted.

Package com.lw.rpc.service;


Import Com.lw.rpc.model.SpiderOrder;


/**

* Provide data Service

*

* @author

*

*/

Public interface Spiderorderservice {


/**

* Generate order Information

* @param spiderorder

* @return

*/

public int Savespiderorder (Spiderorder spiderorder);

/**

* Modify order Information

* @param spiderorder

*/

public void Updatespiderorder (Spiderorder spiderorder);

/**

* Delete Order Information

* @param Spiderid

*/

public void Deletespiderorderbyid (Long spiderid);

}


Package Com.lw.rpc.service.impl;


Import Org.springframework.stereotype.Service;


Import Com.lw.rpc.model.SpiderOrder;

Import Com.lw.rpc.service.SpiderOrderService;


/**

* Order Fulfillment Class

*

* @author

*

*/

@Service ("Spiderorderservice")

public class Spiderorderserviceimpl implements spiderorderservice{


/**

* Generate Order

*/

public int Savespiderorder (Spiderorder spiderorder) {

Specific business logic

System.out.println ("Preservation Method");

return 0;

}


@Override

public void Updatespiderorder (Spiderorder spiderorder) {

TODO auto-generated Method Stub

}


@Override

public void Deletespiderorderbyid (Long spiderid) {

TODO auto-generated Method Stub

}


}


Test method:

Package com.lw.rpc.test;


Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;


Import Com.lw.rpc.model.SpiderOrder;

Import Com.lw.rpc.service.SpiderOrderService;


public class Springaoptest {


public static void Main (string[] args) {

ApplicationContext cxt = new Classpathxmlapplicationcontext ("/spring/spring-applicationcontext.xml");

Spiderorderservice Spiderorderservice = (spiderorderservice) cxt.getbean ("Spiderorderservice");

Spiderorderservice.savespiderorder (New Spiderorder ());

}

}


The output is as follows:

[Main] INFO org.springframework.context.support.classpathxmlapplicationcontext-refreshing org[email protected]5d6f64b1: startup Date [Sun Apr 23:28:47 CST 2017]; Root of context Hierarchy

[Main] INFO org.springframework.beans.factory.xml.xmlbeandefinitionreader-loading XML Bean definitions from class path resource [Spring/spring-applicationcontext.xml]

[Main] INFO org.springframework.beans.factory.config.propertyplaceholderconfigurer-loading properties file from class path resource [properties/datasource/init.properties]

[Main] INFO org.springframework.beans.factory.config.propertyplaceholderconfigurer-loading properties file from class path resource [properties/redis/redis-conf.properties]

[Main] INFO org.springframework.beans.factory.config.propertyplaceholderconfigurer-loading properties file from class path resource [properties/server/server-config.properties]

Before entering the connection point +++++++++++++++

Class Com.lw.rpc.service.impl.SpiderOrderServiceImpl object is using this

[Email protected]

Go to Connection point method

Save method


This article is from the "10093778" blog, please be sure to keep this source http://10103778.blog.51cto.com/10093778/1916509

Spring-aop use Method (ii)

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.