首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

用ProxyFactoryBean创造AOP代理

2013-07-16 
用ProxyFactoryBean创建AOP代理我参照http://uule.iteye.com/blog/869309里面的方法,自己做了一个例子,但

用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">  


<value>a.IPerson</value>  
</property>
<property name="target">
<ref bean="person" />
</property>
<property name="interceptorNames">
<list>
<value>a1</value>
</list>
</property>
</bean>
</beans>
[解决办法]
应该是

IPerson l = factory.getBean("civilian", IPerson.class);

热点排行