AOP is the cut-in for an exception before, during, after, and after running the target method. Any exceptions that occur in the cut-in run do not affect the operation of the target method.
1, define an interface "Iservicename.java"
Package com.zuk.hl.test.springAOP.annotation;
/**
* <p> title:classname </p>
* <p> description:classdescription </p>
* <p > Copyright:openlo.cn Copyright (C) </p>
*
* @author huangl
* @since May 25, 2017 PM 4:48:55
*
/public interface Iservicename {
string setName (string name);
}
2, implement the interface class "Servicename.java"
Package com.zuk.hl.test.springAOP.annotation;
Import org.springframework.stereotype.Component;
/**
* <p> title:classname </p>
* <p> description:classdescription </p>
* <p > Copyright:openlo.cn Copyright (C) </p>
*
* @author huangl
* @since May 24, 2017 PM 3:16:48
*/
@Component Public
class ServiceName implements iservicename{
private String name;
public string SetName (string name) {
System.out.println ("Servicename.setname ():" +name);
this.name = name;
return this.name + "Ldygo";
}
public Boolean throwexcption () throws exception{
if (true) {
System.out.println ("Throwexcption ()");
throw new Exception ("Spring AOP throwadvice demo");
}
return false;
}
@Override public
String toString () {
return ' [servicename:{name: ' +this.name+ '}] ';
}
}
3, implement the interface class "Servicename2.java"
Package com.zuk.hl.test.springAOP.annotation;
Import org.springframework.stereotype.Component;
/**
* <p> title:classname </p>
* <p> description:classdescription </p>
* <p > Copyright:openlo.cn Copyright (C) </p>
*
* @author huangl
* @since May 24, 2017 PM 3:16:48
*/
@Component Public
class ServiceName2 implements iservicename{
private String name;
public string SetName (string name) {
System.out.println ("Servicename2.setname ():" +name);
this.name = name;
return THIS.name + ". Ldygo. ";
}
public Boolean throwexcption () throws exception{
if (true) {
System.out.println ("Throwexcption ()");
throw new Exception ("Spring AOP throwadvice demo");
}
return false;
}
@Override public
String toString () {
return ' [servicename2:{name: ' +this.name+ '}] ';
}
}
4, set an AOP interface "Iaopservice.java"
Package com.zuk.hl.test.springAOP.annotation;
Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.ProceedingJoinPoint;
Import Org.aspectj.lang.annotation.After;
Import org.aspectj.lang.annotation.AfterReturning;
Import org.aspectj.lang.annotation.AfterThrowing;
Import Org.aspectj.lang.annotation.Around;
Import Org.aspectj.lang.annotation.Before;
Import Org.aspectj.lang.annotation.Pointcut; /** * <p> title:iservice </p> * <p> Description: AOP-Oriented Programming interface </p> * <p> Copyright:openlo. CN Copyright (C) </p> * * @author Huangl * @since May 24, 2017 pm 4:19:15 * * Public abstract class Iaopservic e {//Pointcut @Pointcut ("") public void Pointcut () {}/** * <br> pre-notification * <br>object[] Getargs: Return to target method Parameter * <br>signature getsignature: Returns the signature of the target method * <br>object Gettarget: Returns the target object that was woven into the enhanced processing * <br>object Gett His: Returns the proxy object generated by the AOP framework for the target object * * <p> Note:</p> * <br> Even if there is an exception thrown, it does not affect the original method's entry * */@BefoRe ("Pointcut ()") public void Beforeadvice (Joinpoint call) {}/** * <br> post notification * <br> how the target method ends (save completed successfully and In case of exception abort, it will be woven into * */@After ("Pointcut ()") public void Afteradvice (Joinpoint joinpoint) {}/** * notification after return * @param joinpoint * @param retVal * * <br> enhanced processing will only be woven after the target method is successfully completed * */@AfterReturning (returning= "Returnobj", pointcut= "Pointcut ()") public void Afterreturnadvice (Joinpoint call,object returnobj) {}/** * <b
r> Surround Notification * <br>proceedingjoinpoint type, which is a subclass of Joinpoint.
* * <br> do not modify the parameters of the target (method) * Object RESULT1 = Call.proceed ();
* * <br> can modify the parameters of the target (method) by modifying the Call.getargs () method * Object result2 = Call.proceed (Call.getargs ());
* * * */@Around ("Pointcut ()") Public Object Aroundadvice (Proceedingjoinpoint call) throws throwable{return null; }//throws Exception notification @AfterThrowing (pointcut= "pointcut ()", throwing= "ex") public void Afterthrowingadvice (Joinpoint joinpoint,
Exception ex) {}}
5, the implementation of the AOP interface class "Aopserviceimpl.java"
Package com.zuk.hl.test.springAOP.annotation;
Import Java.sql.ResultSet;
Import Java.util.Arrays;
Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.ProceedingJoinPoint;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Pointcut;
Import org.springframework.stereotype.Component; /** * <p> Title:aopserviceimpl </p> * <p> Description: Programming for AOP Facets </p> * <p> Copyright: openlo.cn Copyright (C) </p> * * @author Huangl * @since May 24, 2017 4:25:14 * *//Declare this to be a component @Component ("a Opserviceimpl ")//Declare this is a slice bean @Aspect public class Aopserviceimpl extends iaopservice{//specific interface @Pointcut (" execution (* Com.zuk.hl.test.springAOP.annotation.IServiceName.setName (String)) ")//specific to Method//@Pointcut (" Execution (* Com.zuk.hl.test.springAOP.annotation.ServiceName.setName (String))//specific to the class//@Pointcut ("Execution (* Com.zuk.hl.test.springAOP.annotation.ServiceName.
*(..))") @Override public void Pointcut () {//TODO Auto-generated method stub} @Override public void Beforeadvice (Joinpoint call) {System.out.println ("--" +call
);
System.out.println ("Cut-in object:" +call.gettarget ());
System.out.println ("Cut-in Parameter:" +arrays.tostring (Call.getargs ()));
object[] args = Call.getargs ();
String str = (string) args[0];
System.out.println ("Incoming parameters are:" +str);
str = str + "XXX";
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
System.out.println ("annotation-Pre-Notification": "+ ClassName +" class "+ methodname+" method started ");
} @Override public void Afteradvice (Joinpoint joinpoint) {System.out.println ("Aopserviceimpl.afteradvice ()"); } @Override public void Afterreturnadvice (Joinpoint call, Object returnobj) {System.out.println ("Aopserviceimpl.afte
Rreturnadvice () ");
System.out.println ("Return value:" +returnobj); } @Override Public Object aroundadvice (Proceedingjoinpoint call) throws Throwable {System.out.println ("AOPSErviceimpl.aroundadvice () ");
Object result = null; This.beforeadvice (call);//equivalent to a pre-notification try {System.out.println (">>>>>>>>>>>>
>>>>>> "+call.getargs () [0]);
Object obj = Call.proceed ();
System.out.println ("obj01:" +obj);
Call.getargs () [0] = "HHIHIH";
result = Call.proceed ();
result = Call.proceed (Call.getargs ());
System.out.println ("obj02:" +result); This.afterreturnadvice ();
Equivalent to a post-notification} catch (Throwable e) {//this.afterthrowingadvice ();/////equivalent to an exception thrown after notification of throw e; } finally {//this.afterreturnadvice ();///equivalent to the final notification System.out.println (">>>>>>>>>>>&
Gt;>> final ");
} return result; } @Override public void Afterthrowingadvice (Joinpoint joinpoint, Exception ex) {System.out.print ("Aopserviceimpl.aft
Erthrowingadvice () ");
System.out.println ("--" +ex.getmessage ());
}
}
6. Test Class "Serviceaspecttestjunit.java"
Package com.zuk.hl.test.springAOP.annotation;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * <p> title:serviceaspecttestjunit </p> * <p> Description: Annotated way to use AOP tangent test program </p> * <p& Gt Copyright:openlo.cn Copyright (C) </p> * * @author Huangl * @since May 24, 2017 pm 3:09:34 */@RunWith (Spring Junit4classrunner.class) @ContextConfiguration (locations = "Classpath:applicationContextAOP2.xml") public class
Serviceaspecttestjunit {@Autowired private ServiceName us;
@Autowired private ServiceName2 US2;
@Test public void Testaop () throws exception{//system.in.read ();
Thread.Sleep (1000);
Us.setname ("hello.");
String str = us.getname ();
System.out.println ("Set the value--" +us.tostring ()); Us.throwexcption ();
Us2.setname ("Biu.");
System.out.println ("Set Value 2-->" +us2.tostring ());
}
}
7. Configuration file "Applicationcontextaop2.xml"
<?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:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans HT
Tp://www.springframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/ TX http://www.springframework.org/schema/tx/spring-tx-3.2.xsd Http://www.springframework.org/sch Ema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!--activating component scanning function, Automatically scans the components configured via annotations under Package CN.YSH.STUDIO.SPRING.AOP and its sub-packages--
<context:component-scan base-package= "Com.zuk.hl.test.springAOP." /> <!--Activate auto-proxy function--<aop:aspectj-autoproxy proxy-target-class= "true"/> </beans>