Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

java code for Proxy Factory Bean

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/



///////////////////////////////////////////////////////////////////////////////////////
//File:pfb.xml






















advice










advisor













.*foo.*







///////////////////////////////////////////////////////////////////////////////////////

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyAdvice implements MethodBeforeAdvice {

public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Executing: " + method);
}

}

///////////////////////////////////////////////////////////////////////////////////////
public class MyBean {

private MyDependency dep;

public void execute() {
dep.foo();
dep.bar();
}

public void setDep(MyDependency dep) {
this.dep = dep;
}
}



///////////////////////////////////////////////////////////////////////////////////////

public class MyDependency {

public void foo() {
System.out.println("foo()");
}

public void bar() {
System.out.println("bar()");
}
}


///////////////////////////////////////////////////////////////////////////////////////

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ProxyFactoryBeanExample {

public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"build/pfb.xml");

MyBean bean1 = (MyBean)ctx.getBean("myBean1");
MyBean bean2 = (MyBean)ctx.getBean("myBean2");

System.out.println("Bean 1");
bean1.execute();

System.out.println("\nBean 2");
bean2.execute();
}
}

No comments: