Java Reference
In-Depth Information
public double kilometerToMile(double kilometer) {
double mile = kilometer * 0.62;
System.out.println(kilometer + " kilometer = " + mile + " mile");
return mile;
}
}
To enable AspectJ annotation support for this application, you just define an empty XML
element, <aop:aspectj-autoproxy> , in your bean configuration file. Moreover, you must add the
aop schema definition to your <beans> root element. When the Spring IoC container notices the
<aop:aspectj-autoproxy> element in your bean configuration file, it will automatically create proxies
for your beans that are matched by your AspectJ aspects.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop=" http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<bean id="arithmeticCalculator"
class="com.apress.springenterpriserecipes.
calculator.ArithmeticCalculatorImpl" />
<bean id="unitCalculator"
class="com.apress.springenterpriserecipes.calculator.UnitCalculatorImpl" />
</beans>
1-11. Declaring Aspects with AspectJ Annotations
Problem
Since merging with AspectWerkz in AspectJ version 5, AspectJ supports aspects written as POJOs
annotated with a set of AspectJ annotations. Aspects of this kind are also supported by the Spring AOP
framework, although they must be registered in the Spring IoC container to take effect.
Solution
You register AspectJ aspects in Spring simply by declaring them as bean instances in the IoC container.
With AspectJ enabled in the Spring IoC container, it will create proxies for your beans that are matched
by your AspectJ aspects.
Written with AspectJ annotations, an aspect is simply a Java class with the @Aspect annotation. An
advice is a simple Java method with one of the advice annotations. AspectJ supports five types of advice
annotations: @Before , @After , @AfterReturning , @AfterThrowing , and @Around .
 
Search WWH ::




Custom Search