在spring 中,如何用cglib进行aop代理,
在网上看到需要设置proxyTargetClass为true,可是我试了,怎么也不行,急死了,谁给个例子,我的程序如下:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessTarget" class="aop.BusinessInterfaceImpl"/>
<bean id="myInterceptor" class="Intercptor.MyInterceptor"/>
<bean id="businessBean"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass" value="true"/>
<property name="target">
<ref local="businessTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
</list>
</property>
</bean>
</beans>
代理类:
package aop;
public class BusinessInterfaceImpl {
public void hello() {
System.out.println("hello Spring AOP.");
}
}
拦截类:
package Intercptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable{
System.out.println("Beginning method (1): " +
methodInvocation.getMethod().getDeclaringClass() + "." +
methodInvocation.getMethod().getName() + "()");
long startTime = System.currentTimeMillis();
try{
Object result = methodInvocation.proceed();
return result;
}finally{
System.out.println("Ending method (1): " +
methodInvocation.getMethod().getDeclaringClass() + "." +
methodInvocation.getMethod().getName() + "()");
System.out.println("Method invocation time (1): " +
(System.currentTimeMillis() - startTime) + " ms.");
}
}
}
测试类:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import aop.BusinessInterfaceImpl;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=new
FileSystemXmlApplicationContext("bean.xml");
BusinessInterfaceImpl action = (BusinessInterfaceImpl) ctx.getBean("businessBean");
action.hello();
}
}
报类型转换错误!!!
如果谁要是有现成的cglib代理方式的例子,给我一个也可以!!
多谢了