Example and Steps for Creating Advice-2
Step3:- Following is the Aspect bean which has method Before Advice which will be called before the testAdvice method of the target object.
package com.visualbuilder.aop;
public class AdviceClass {
public void beforeMethod(){
System.out.println("This is the before method of Advice Class");
}
} |
Step4:-Declare all the beans in the config file i.e. beans.xml
<!-- this is the object that will be proxied by Spring's AOP infrastructure -->
<bean id="service" class="com.visualbuilder.aop.ServiceImpl"/>
<!-- this is the actual advice itself -->
<bean id="advice" class="com.visualbuilder.aop.AdviceClass"/>
|
Step5:-Declare the <aop:config> and <aop:aspect> with pointcuts and advice type settings.
<aop:config>
<aop:aspect ref="advice">
<aop:pointcut id="beforeMethodExecution" expression="execution(* com.visualbuilder.aop.Service.testAdvice())"/>
<aop:before pointcut-ref="beforeMethodExecution" method="beforeMethod"/>
</aop:aspect>
</aop:config>
|
So below is given the complete beans.xml File:-
<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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- this is the object that will be proxied by Spring's AOP infrastructure -->
<bean id="service" class="com.visualbuilder.aop.ServiceImpl" />
<!-- this is the actual advice itself -->
<bean id="advice" class="com.visualbuilder.aop.AdviceClass" />
<aop:config>
<aop:aspect ref="advice">
<aop:pointcut id="beforeMethodExecution"
expression="execution(* com.visualbuilder.aop.Service.testAdvice())" />
<aop:before pointcut-ref="beforeMethodExecution"
method="beforeMethod" />
</aop:aspect>
</aop:config>
</beans>
|