In this tutorial you will learn how to use Spring AOP ProxyFactory.
In this tutorial you will learn how to use Spring AOP ProxyFactory.FactoryBean is used for implementing the source AOP proxies from a Spring BeanFactory. This bean should be type of interceptor or advisor. Example given below illustrates this concept.
DependencyBean.java
package roseindia.net.bean; public class DependencyBean { public void sayHi() { System.out.println("Hello "); } public void greet() { System.out.println("Have a nice Day......"); } }
SimpleBean.java
package roseindia.net.bean; public class SimpleBean { private DependencyBean dependency; public void test() { dependency.sayHi(); dependency.greet(); } public void setDependency(DependencyBean dependency) { this.dependency = dependency; } }
SimpleAdvice.java
package roseindia.net.advice; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class SimpleAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object object) throws Throwable { // TODO Auto-generated method stub System.out.println("Currently Processing " + method); } }
MainClaz.java
package roseindia.net.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import roseindia.net.bean.SimpleBean; public class MainClaz { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "classpath:./config.xml"); SimpleBean simpleBean1 = (SimpleBean) context.getBean("Bean1"); simpleBean1.test(); } }
config.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="Bean1" class="roseindia.net.bean.SimpleBean"> <property name="dependency"> <ref local="myDependency1" /> </property> </bean> <bean id="dependencyTarget" class="roseindia.net.bean.DependencyBean" /> <bean id="myDependency1" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="dependencyTarget" /> </property> <property name="interceptorNames"> <list> <value>advice</value> </list> </property> </bean> <bean id="myDependency2" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="dependencyTarget" /> </property> <property name="interceptorNames"> <list> <value>advisor</value> </list> </property> </bean> <bean id="advice" class="roseindia.net.advice.SimpleAdvice" /> <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice"> <ref local="advice" /> </property> <property name="pointcut"> <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern"> <value>.*sayHi.*</value> </property> </bean> </property> </bean> </beans> |
Currently Processing public void
roseindia.net.bean.DependencyBean.sayHi() Hello Currently Processing public void roseindia.net.bean.DependencyBean.greet() Have a nice Day...... |