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:
<!--component Scan--
<context:component-scan base-package= "Com.lw.rpc"
Use-default-filters= "false" >
<context:include-filter type= "Annotation"
expression= "Org.springframework.stereotype.Repository"/>
<context:include-filter type= "Annotation"
expression= "Org.springframework.stereotype.Service"/>
</context:component-scan>
<!--notice Class--
<bean id= "ASPECTJADVICEAOP" class= "Com.lw.rpc.test.AspectJAdviceAop"/>
<aop:config>
<aop:aspect id= "Useaapect" ref= "ASPECTJADVICEAOP" >
<!--configuring Pointcuts--and
<aop:pointcut expression= "Execution (* com.lw.rpc. *.*(..))" Id= "Point_cut"/>
<!--front-facing notifications-
<aop:before method= "Beforeadvice" pointcut-ref= "Point_cut"/>
<!--<aop:pointcut id= "Except_add" expression= "Execution (* com.lw.rpc). *.*(..)))" />--
<!--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.
<!--<aop:after-returning method= "Afteradvice" pointcut-ref= "point_cut" returning= "result"/>-
<!--surround-
<!--<aop:around method= "Aroundadvice" pointcut-ref= "Point_cut"/>--
<!--throw-in
<!--<aop:after-throwing method= "Afterthrow" pointcut-ref= "Point_cut" throwing= "E"/>--
</aop:aspect>
</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)