private MyDependency dep;
public void execute() {
dep.foo();
dep.bar();
}
public void setDep(MyDependency dep) {
this.dep = dep;
}
}
For this example, we are going to create two proxies for a single MyDependency instance, both with
the same basic advice shown in Listing 7-13.
Listing 7-13. The MyAdvice Class
package com.apress.prospring3.ch7.pfb;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Executing: " + method);
}
}
The first proxy will just advise the target using the advice directly; thus, all methods will be advised.
For the second proxy, we will configure a AspectJExpressionPointcut and a DefaultPointcutAdvisor so
that only the foo() method of the MyDependency class is advised. To test the advice, we will create two
bean definitions of type MyBean, each of which will be injected with a different proxy. Then we will invoke
the execute() method on each of these beans and observe what happens when the advised methods on
the dependency are invoked.
Listing 7-14 shows the configuration for this example (pfb.xml).
Listing 7-14. Declarative AOP Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="myBean1" class="com.apress.prospring3.ch7.pfb.MyBean">
<property name="dep">
<ref local="myDependency1"/>
</property>
</bean>
<bean id="myBean2" class="com.apress.prospring3.ch7.pfb.MyBean">
<property name="dep">
<ref local="myDependency2"/>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home