It means we will not use aspectj.
We must therefore create our proxy and our interceptors and manually wrap the target by the interceptor suitable.
We must therefore create our proxy and our interceptors and manually wrap the target by the interceptor suitable.
Beginning with the Create Interceptors:
1) Execute something before the execution of the method Target:
1) Execute something before the execution of the method Target:
1: /*
2: * To change this template, choose Tools | Templates
3: * and open the template in the editor.
4: */
5: package slm.abdennour.aop.interceptors;
7: import java.lang.reflect.Method;
8: import org.springframework.aop.MethodBeforeAdvice;
9: /**
10: *
11: * @author Abdennour toumi
12: * @since 28 mars 2012 / 07:40:46
13: */
14: public class AbdennourBeforeMethodExec implements MethodBeforeAdvice {
16: public void before(Method method, Object[] os, Object o) throws Throwable {
17: System.out.println("Abdennour you said :pay attention,Your Methode "+method.getName()+" will be executed");
18:
19: }
20:
21: }
2)Execute something after a return of the method Target:
3)Execute something After Throws(Bad return) of the method Target:
4)Execute something before&after the execution of the method Target:
Target Class:(Which is wrapped by Proxy)
Define Proxy ,Target & Interceptors In Configuration Spring Xml :
1)Before Execution :"Spring-Customer.xml"
2)After Return:"Spring-Customer.xml"
3)After Throws:"Spring-Customer.xml"
4)Before&After:"cfgForAroundAdvice.xml"
<bean id="customer" class="slm.abdennour.core.CustomerService" p:name="Omar" p:url="http://abdennour-insat.blogspot.com" />
<!--Interceptor -->
<bean id="aroundBean" class="slm.abdennour.aop.interceptors.AbdennourBothBeforeAfter" />
<!-- PROXY -->
<bean id="aroundProxyCustomer" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
<property name="interceptorNames" >
<list>
<value>aroundBean</value>
</list>
</property>
</bean>
|
package slm.abdennour.aop.interceptors; |
4)Execute something before&after the execution of the method Target:
|
Target Class:(Which is wrapped by Proxy)
5: package slm.abdennour.core;
7: /**
8: *
9: * @author Abdennour toumi
10: * @since 28 mars 2012 / 07:41:36
11: */
12: public class CustomerService {
14: private String name;
15: private String url;
17: public void setName(String name) {
18: this.name = name;
19: }
21: public void setUrl(String url) {
22: this.url = url;
23: }
25: public void printName() {
26: System.out.println("Customer name : " + this.name);
27: }
29: public void printURL() {
30: System.out.println("Customer website : " + this.url);
31: }
33: public void printThrowException() {
34: throw new IllegalArgumentException();
35: }
36: }
Define Proxy ,Target & Interceptors In Configuration Spring Xml :
1)Before Execution :"Spring-Customer.xml"
2)After Return:"Spring-Customer.xml"
<!--Interceptor -->
<bean id="afterReturnMethod" class="slm.abdennour.aop.interceptors.AbdennourAfterMethodReturn" />
<!-- PROXY -->
<bean id="proxyCustomerAfter" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
<property name="interceptorNames" >
<list>
<value>afterReturnMethod</value>
</list>
</property>
</bean>
3)After Throws:"Spring-Customer.xml"
4)Before&After:"cfgForAroundAdvice.xml"
<bean id="customer" class="slm.abdennour.core.CustomerService" p:name="Omar" p:url="http://abdennour-insat.blogspot.com" />
<!--Interceptor -->
<bean id="aroundBean" class="slm.abdennour.aop.interceptors.AbdennourBothBeforeAfter" />
<!-- PROXY -->
<bean id="aroundProxyCustomer" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
<property name="interceptorNames" >
<list>
<value>aroundBean</value>
</list>
</property>
</bean>
Test The For Interceptors: import org.springframework.context.ApplicationContext; }
|
No comments:
Post a Comment