日期:2014-05-18 浏览次数:20710 次
public class TestBeanA {
public void sayHello() {
System.out.println("Hello, Autoproxy");
}
}
public class BeanB {
public void sing() {
System.out.println("有个大中国啊,我们是一家!");
}
}
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println(method.getName() + "(),方法即将运行...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanA" class="com.chenjb.aop.autoproxy.TestBeanA"/>
<bean id="beanB" class="com.chenjb.aop.autoproxy.BeanB"/>
<bean id="beforeAdvice" class="com.chenjb.aop.autoproxy.BeforeAdvice"/>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>Bean*</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
</list>
</property>
</bean>
</beans>
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"autoproxyname.xml", TestBeforeAdvice.class);
TestBeanA beanA = ctx.getBean("beanA", TestBeanA.class);
beanA.sayHello();
BeanB beanB = ctx.getBean("beanB", BeanB.class);
beanB.sing();
}