Spring altogether provides five types of notifications:
| Notification type |
Interface |
Describe |
| Around Surround Notification |
Org.aopalliance.intercept.MethodInterceptor |
Intercept the target method call |
| Before forward notification |
Org.springframework.aop.MethodBeforeAdvice |
Called before the target method call |
| After post notice |
Org.springframework.aop.AfterReturningAdvice |
Called after the target method call |
| Throws Exception Notification |
Org.springframework.aop.ThrowsAdvice |
Called when the target method throws an exception |
The other is to introduce notifications to define pointcuts.
The previous article has already introduced the predecessor notice, is before the method is called executes the predecessor notification first
Now let me introduce you to other notices:
1, Post notice:
The writing of the Post notification class:
Package COM.CDTAX.AOP;
Import Java.lang.reflect.Method;
Import Org.springframework.aop.AfterReturningAdvice;
public class Myafterreturningadvice implements Afterreturningadvice
{
@Override public
Void Afterreturning (Object ReturnValue, Method method,
object[] args, object target) throws Throwable
{
SYSTEM.OUT.PRINTLN ("Post notification is: Close resource,");
}
Configuration in Beans.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/ Schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--Configure the objects to be represented--> <bean id=" Test1service "class=" Com.cdtax.aop.Test1Service "> <property name=" name "value=" Xiaoming "></property> </ Bean> <!--Configure forward notification--> <bean id= "Mymethodbeforeadvice" class= "Com.cdtax.aop.MyMethodBeforeAdvice" >< /bean> <!--configuration Post notification--> <bean id= "Myafterreturningadvice" class=P.myafterreturningadvice "></bean> <!--Configure proxy objects--> <bean id=" Proxyfactorybean "class=" Org.springframework.aop.framework.ProxyFactoryBean "> <!--Proxy interface set--> <property name=" Proxyinterfaces " > <list> <value>com.cdtax.aop.TestServiceInter</value> <value>com.cdtax.aop.testservic Einter2</value> </list> </property> <!--weaving notices into proxy objects | Interceptor Name set--> <property name= "Interceptornames" > <list> <!--is equivalent to associating Mymethodbeforeadvice forward notifications with proxy objects and we Notice can be regarded as interceptors, the STRUTS2 core is interceptor--> <value>myMethodBeforeAdvice</value> <!--weaving Post notification--> <va Lue>myafterreturningadvice</value> </list> </property> <!--Configuring the proxy object, you can specify--> <PR Operty name= "target" ref= "Test1service" > </property> </bean> </beans>
Test class:
Package COM.CDTAX.AOP;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1
{public
static void Main (string[] args)
{
ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/cdtax/aop/beans.xml"); test1service ts = (test1service) ac.getbean ("Test1service"); Ts.sayhello ();
Testserviceinter ts1 = (testserviceinter) Ac.getbean ("Proxyfactorybean");
Ts1.sayhello ();
((TestServiceInter2) ts1). Saybye ();
}
Execution results:
-----------------------------
Pre-notification: Log ... SayHello
Hai Xiao Ming
The post notification is invoked: close the resource,
-----------------------------
Pre-notification: Log ... Saybye
Bye Xiao Ming
The post notification is invoked: close the resource,
2. Surround Notification
Package COM.CDTAX.AOP;
Import Org.aopalliance.intercept.MethodInterceptor;
Import org.aopalliance.intercept.MethodInvocation;
public class Mymethodinterceptor implements Methodinterceptor
{
@Override the public
Object invoke ( Methodinvocation arg0) throws Throwable
{
System.out.println ("Surround notification is invoked: Execute before calling method ");
Object obj = Arg0.proceed ();
SYSTEM.OUT.PRINTLN ("Surround notification called: Execute after calling method ");
return obj;
}
}
Configure Beans.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/ Schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--Configure the objects to be represented--> <bean id=" Test1service "class=" Com.cdtax.aop.Test1Service "> <property name=" name "value=" Xiaoming "></property> </ Bean> <!--Configure forward notification--> <bean id= "Mymethodbeforeadvice" class= "Com.cdtax.aop.MyMethodBeforeAdvice" >< /bean> <!--configuration Post notification--> <bean id= "Myafterreturningadvice" class=P.myafterreturningadvice "></bean> <!--Configure wrapping notification--> <bean id=" Mymethodinterceptor "class=" Com.cdtax.aop.MyMethodInterceptor "></bean> <!--Configure proxy objects--> <bean id=" Proxyfactorybean "class=" Org.springframework.aop.framework.ProxyFactoryBean "> <!--Proxy interface set--> <property name=" Proxyinterfaces " > <list> <value>com.cdtax.aop.TestServiceInter</value> <value>com.cdtax.aop.testservic Einter2</value> </list> </property> <!--weaving notices into proxy objects | Interceptor Name set--> <property name= "Interceptornames" > <list> <!--is equivalent to associating Mymethodbeforeadvice forward notifications with proxy objects and we Notice can be regarded as interceptors, the STRUTS2 core is interceptor--> <value>myMethodBeforeAdvice</value> <!--weaving Post notification--> <va
Lue>myafterreturningadvice</value> <!--weaving Surround notification--> <value>myMethodInterceptor</value> </list> </property> <!--Configuring the proxy object, you can specify--> <property name= "Target"ref= "Test1service" > </property> </bean> </beans>
Execute APP1 Test class, execute result:
-----------------------------
Pre-notification: Log ... SayHello
Wrapping notification called: Executing before calling a method
Hai Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
-----------------------------
Pre-notification: Log ... Saybye
Wrapping notification called: Executing before calling a method
Bye Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
3, Abnormal Notice:
Modify the Test1service class,
Package COM.CDTAX.AOP;
public class Test1service implements Testserviceinter,testserviceinter2
{
private String name;
Public String getName ()
{return
name;
}
public void SetName (String name)
{
this.name = name;
}
@Override public
void SayHello ()
{
System.out.println ("Hai" + name);
}
@Override public
void Saybye ()
{
System.out.println ("Bye" + name);
int i = 9/0;
}
}
Add a 0 exception to the Saybye () method
Write Exception Notification:
Package COM.CDTAX.AOP;
Import Java.lang.reflect.Method;
Import Org.springframework.aop.ThrowsAdvice;
public class Mythrowsadvice implements Throwsadvice
{public
void afterthrowing (method m,object[] Os,object Target,exception throwable)
{
System.out.println ("Exception Notification: Out of event" + throwable.getmessage ());
}
Configure in Beans.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/ Schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--Configure the objects to be represented--> <bean id=" Test1service "class=" Com.cdtax.aop.Test1Service "> <property name=" name "value=" Xiaoming "></property> </ Bean> <!--Configure forward notification--> <bean id= "Mymethodbeforeadvice" class= "Com.cdtax.aop.MyMethodBeforeAdvice" >< /bean> <!--configuration Post notification--> <bean id= "Myafterreturningadvice" class=P.myafterreturningadvice "></bean> <!--Configure wrapping notification--> <bean id=" Mymethodinterceptor "class=" Com.cdtax.aop.MyMethodInterceptor "></bean> <!--configuration exception notification--> <bean id=" Mythrowsadvice "class=" Com.cdtax.aop.MyThrowsAdvice "></bean> <!--Configure proxy objects--> <bean id=" Proxyfactorybean "class=" Org.springframework.aop.framework.ProxyFactoryBean "> <!--Proxy interface set--> <property name=" Proxyinterfaces " > <list> <value>com.cdtax.aop.TestServiceInter</value> <value>com.cdtax.aop.testservic Einter2</value> </list> </property> <!--weaving notices into proxy objects | Interceptor Name set--> <property name= "Interceptornames" > <list> <!--is equivalent to associating Mymethodbeforeadvice forward notifications with proxy objects and we Notice can be regarded as interceptors, the STRUTS2 core is interceptor--> <!--weaving forward notification--> <value>myMethodBeforeAdvice</value> <! --weaving Post notification--> <value>myAfterReturningAdvice</value> <!--weaving Surround notification--> <VALUE>MYMEthodinterceptor</value> <!--weaving anomaly notification--> <value>myThrowsAdvice</value> </list> </property> <!--Configure the proxy object, you can specify--> <property name= "target" ref= "Test1service" > </property>
; </bean> </beans>
Run APP1 test class results:
-----------------------------
Pre-notification: Log ... SayHello
Wrapping notification called: Executing before calling a method
Hai Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
-----------------------------
Pre-notification: Log ... Saybye
Wrapping notification called: Executing before calling a method
Bye Xiao Ming
Exception notification: Out of the event/by zero
Exception in thread ' main ' java.lang.ArithmeticException:/by zero
At Com.cdtax.aop.Test1Service.sayBye (test1service.java:28)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:39)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:25)
At Java.lang.reflect.Method.invoke (method.java:597)
At Org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection (aoputils.java:307)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint (Reflectivemethodinvocation.java : 182)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:149)
At Org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke (throwsadviceinterceptor.java:126)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:171)
At Com.cdtax.aop.MyMethodInterceptor.invoke (mymethodinterceptor.java:13)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:171)
At Org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke ( AFTERRETURNINGADVICEINTERCEPTOR.JAVA:50)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:171)
At Org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke ( METHODBEFOREADVICEINTERCEPTOR.JAVA:50)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:171)
At Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (jdkdynamicaopproxy.java:204)
At $Proxy 0.sayBye (Unknown Source)
At Com.cdtax.aop.App1.main (app1.java:17)
4, the last one is to introduce a notification, the introduction of the notification does not need to write the appropriate class, only need to configure, the purpose is to specify which methods need to perform the corresponding notification, such as, we want to specify only the SayHello () method to perform the predecessor notice,
The following configurations are configured in Beans.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/ Schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--Configure the objects to be represented--> <bean id=" Test1service "class=" Com.cdtax.aop.Test1Service "> <property name=" name "value=" Xiaoming "></property> </ Bean> <!--Configure forward notification--> <bean id= "Mymethodbeforeadvice" class= "Com.cdtax.aop.MyMethodBeforeAdvice" >< /bean> <!--configuration Post notification--> <bean id= "Myafterreturningadvice" class=P.myafterreturningadvice "></bean> <!--Configure wrapping notification--> <bean id=" Mymethodinterceptor "class=" Com.cdtax.aop.MyMethodInterceptor "></bean> <!--configuration exception notification--> <bean id=" Mythrowsadvice "class=" Com.cdtax.aop.MyThrowsAdvice ></bean> <!--to define a pointcut for a forward notification (reference notification)--> <bean id= " Mymethodbeforeadvicefilter "class=" Org.springframework.aop.support.NameMatchMethodPointcutAdvisor "> < Property name= "Advice" ref= "Mymethodbeforeadvice" ></property> <property name= "Mappednames" > <list > <value>sayHello</value> </list> </property> </bean> <!--Configuring proxy objects--> <be An id= "Proxyfactorybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" > <!--Agent interface set--> < Property name= "Proxyinterfaces" > <list> <value>com.cdtax.aop.TestServiceInter</value> <val Ue>com.cdtax.aop.testserviceinter2</value> </list> </property> <!--weave the notice intoTo Proxy objects | Interceptor Name set--> <property name= "Interceptornames" > <list> <!--is equivalent to associating Mymethodbeforeadvice forward notifications with proxy objects and we Notice can be regarded as interceptor, struts2 Core is interceptor--> <!--<value>myMethodBeforeAdvice</value>--> <!--using custom Pointcut Pre-notification--> <value>myMethodBeforeAdviceFilter</value> <!--weaving Post notification--> <value>myafterret Urningadvice</value> <!--woven Surround notification--> <value>myMethodInterceptor</value> <!--weaving exception notification
--> <value>myThrowsAdvice</value> </list> </property> <!--Configuring the proxy object, you can specify--> <property name= "target" ref= "Test1service" > </property> </bean> </beans>
Change the Saybye () method of the Test1service, remove the 0 exception, and perform the APP1 test class result:
-----------------------------
Pre-notification: Log ... SayHello
Wrapping notification called: Executing before calling a method
Hai Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
Wrapping notification called: Executing before calling a method
Bye Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
As you can see, only the SayHello () method performs a predecessor notification, and the Saybye () method does not perform
This is the reference notification used to define the pointcut.
5, about the type of ts1,
Package COM.CDTAX.AOP;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1
{public
static void Main (string[] args)
{
ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/cdtax/aop/beans.xml"); test1service ts = (test1service) ac.getbean ("Test1service"); Ts.sayhello ();
Testserviceinter ts1 = (testserviceinter) Ac.getbean ("Proxyfactorybean");
Ts1.sayhello ();
System.out.println ("Ts1 type:" + ts1);
((TestServiceInter2) ts1). Saybye ();
}
We get ts1 through Getbean ("Proxyfactorybean"); The class of Proxyfactorybean is Org.springframework.aop.framework.ProxyFactoryBean, so the type of ts1 is it too. Run the above program, the result:
-----------------------------
Pre-notification: Log ... SayHello
Wrapping notification called: Executing before calling a method
Hai Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
Wrapping notification called: Executing before calling a method
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
Type of Ts1: COM.CDTAX.AOP.TEST1SERVICE@1982FC1
Wrapping notification called: Executing before calling a method
Bye Xiao Ming
Wrapping notification is called: executing after calling a method
The post notification is invoked: close the resource,
As you can see, the type of TS1 is Com.cdtax.aop.Test1Service, which is generated by the Proxyfactorybean dynamic proxy. Dynamic proxy method is implemented in Org.springframework.aop.framework.ProxyFactoryBean.
Spring creates agents at runtime without special compilers, and spring has two ways of acting:
(1) If the target object implements several interfaces, spring uses the JDK's Java.lang.reflect.Proxy class proxy class to let spring dynamically generate a new class, he implements the required interface, weaves the notification, and proxies all requests to the target object.
(2) If the target object does not implement any interfaces, spring uses the Cglib library to generate a subclass of the target object. You need to be aware of this when you use this method:
1 creating proxies for interfaces takes precedence over creating proxies for classes because of a more loosely coupled system. A class broker is also notified of a Third-party class library that has legacy systems or interfaces that cannot be implemented, in the same way as Beiyongfang ' an.
2 The method marked as final cannot be notified. Spring is the generation of subclasses for the target class. Any method that needs to be notified is overwritten. Weave the notifications into. The final method is not allowed to be overridden.
Spring implements the AOP federated interface.
Spring only supports method connection points: No attribute access point is provided, and spring's view is that attribute interception destroys encapsulation. The object-oriented concept is that objects work on their own, and other objects can only be used to get results by means of methods.
6. For introducing notifications, you can use regular expressions when defining slices, such as using say* when defining pointcuts, representing all methods that begin with say.