Java Reference
In-Depth Information
Spring's AOP infrastructure has some restrictions compared to full AOP solutions. Spring
restricts pointcuts to only public method boundaries on Spring-managed beans. I therefore
need to add the
POJO
bean to Spring's configuration file. I also need to tell Spring to re-
cognize the
@Aspect
annotation and to generate the needed proxy. The resulting bean
configuration file is presented in the following listing.
Listing 7.16. The Spring bean configuration file for AOP
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
... namespace declarations elided ... >
<aop:aspectj-autoproxy />
<bean id=
"tracker"
class=
"mjg.aspects.ChangeLogger"
/>
<bean id=
"pojo"
class=
"mjg.POJO"
p:one=
"1"
p:two=
"2"
p:three=
"3"
/>
</beans>
The
aop
namespace provides the
<aspect-autoproxy>
element, which tells Spring
to generate proxies for all classes annotated with
@Aspect
. The
tracker
bean is the
Java aspect shown previously. The
pojo
bean is the
POJO
class just discussed.
Now I need to call the
set
methods in order to see the aspect in action. The next listing
shows a test case based on JUnit 4 that uses Spring's JUnit 4 test runner, which caches the
application context in between tests.
Listing 7.17. A JUnit 4 test case to exercise the POJO
package mjg;
import static org.junit.Assert.*;
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;
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class POJOTest {
@Autowired
private POJO pojo;
@Test
public void callSetters() {