日期:2014-05-17  浏览次数:20766 次

用ProxyFactoryBean创建AOP代理
我参照http://uule.iteye.com/blog/869309里面的方法,自己做了一个例子,但是始终不能出来拦截器的运行结果,程序也不报错,不知道是什么原因啊。我用的是Spring3.2.3的环境。
package a;

public interface IPerson {
public void sayHello();
}
-----------------------------------------------
package a;

public class Person implements IPerson {
public void sayHello()
{
System.out.println("Hello1");
}
}
----------------------------------------------
package a;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class myaspect implements MethodBeforeAdvice {

@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("运行前检查");
}

}
----------------------------------------------
package a;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
public static void main(String[] args)
{
BeanFactory factory= new ClassPathXmlApplicationContext("test.xml");
Person l=factory.getBean("person",a.Person.class);
l.sayHello();
}
}
----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    default-autowire="autodetect">
<!-- 拦截器对象 -->
<bean id="a1" class="a.myaspect"/>
<bean id="person" class="a.Person"/>
<bean id="civilian"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">