Listing 7-30. Testing AspectJ Annotation
package com.apress.prospring3.ch7.aspectjannotation;
import org.springframework.context.support.GenericXmlApplicationContext;
public class AspectJAnnotationExample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:aspectjannotation.xml");
ctx.refresh();
MyBean myBean = (MyBean) ctx.getBean("myBean");
myBean.execute();
}
}
Running the program will yield the same results as the aop namespace example:
Before execution: com.apress.prospring3.ch7.aspectjannotation.MyDependency foo argument: 100
foo(int): 100
After execution: com.apress.prospring3.ch7.aspectjannotation.MyDependency foo argument: 100
Executing: com.apress.prospring3.ch7.aspectjannotation.MyDependency foo argument: 101
Before execution: com.apress.prospring3.ch7.aspectjannotation.MyDependency foo argument: 101
foo(int): 101
After execution: com.apress.prospring3.ch7.aspectjannotation.MyDependency foo argument: 101
bar()
Considerations for Declarative Spring AOP Configuration
So far, we have discussed three different ways of declaring Spring AOP configuration, including the
ProxyFactoryBean, the aop namespace, and @AspectJ-style annotations. We believe you will agree that the
aop namespace is much simpler than the ProxyFactoryBean. So, the general question is, do you use aop
namespace or @AspectJ-style annotations?
If your Spring application is XML configuration based, then using the aop namespace approach is a
natural choice, because it keeps the AOP and DI configuration styles consistent. On the other hand, if
your application is mainly annotation based, then use the @AspectJ annotation. The @AspectJ annotation
approach also has the advantage that all the aspect-related information is encapsulated in one module,
which is easier to manage.
Moreover, there are some other differences between the aop namespace and @AspectJ annotation
approaches:
The pointcut expression syntax has some minor differences (e.g., in the previous
·
discussions, we need to use and in the aop namespace, but && in @AspectJ annotation).
The aop namespace approach supports only the "singleton" aspect instantiation
·
model.
In the aop namespace, you can't "combine" multiple pointcut expressions. For
·
example, in the example using @AspectJ, we can combine the two pointcut
definitions (i.e., fooExecution(intValue) && inMyDependency()) in the before and
around advice. But you can't do this when using the aop namespace, and you need
to create a new pointcut expression that combines the matching conditions.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home