Around Advice Example-2
All around advices are declared with <aop:around> tag The following is the bean.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="methodPointcut"
expression="execution(* com.visualbuilder.aop.ServiceImpl.actualMethod())" />
<aop:around
pointcut-ref="methodPointcut" method="pointcutMethod" />
</aop:aspect>
</aop:config>
</beans>
|
MainClass.java
package com.visualbuilder.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class MainClass {
public static void main(String[] args) throws Exception {
BeanFactory ctx = new ClassPathXmlApplicationContext("com/visualbuilder/aop/bean.xml");
Service object = (Service) ctx.getBean("service");
object.actualMethod();
}
}
|
Output:-
The following is the ouput for the above example.
Sep 21, 2008 12:14:11 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Sep 21 12:14:11 IST 2008]; root of context hierarchy
Sep 21, 2008 12:14:12 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/visualbuilder/aop/bean.xml]
Sep 21, 2008 12:14:14 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@763f5d
Sep 21, 2008 12:14:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@763f5d: defining beans [service,advice,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,methodPointcut]; root of factory hierarchy
This is Before Advice Text
This is the Actual Method in the service class.
This is After Retruning Advice Text
This is After (finally) Advice Text
|