在spring 中,如何用cglib进行aop代理,

sparkcn 2006-12-06 06:01:12
在网上看到需要设置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代理方式的例子,给我一个也可以!!


多谢了
...全文
585 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whb123 2006-12-15
  • 打赏
  • 举报
回复
JRun装在XP不能启动服务,是不是不能用于XP系统?
whb123 2006-12-15
  • 打赏
  • 举报
回复
我有如此错误信息,请专家不吝赐教,多谢!:

2006-12-15 14:20:14 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
信息: CGLIB2 not available: proxyTargetClass feature disabled
2006-12-15 14:20:15 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
Exception in thread "main" org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:72)
at org.springframework.aop.framework.AdvisedSupport.createAopProxy(AdvisedSupport.java:487)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:70)
at com.apress.prospring.aop.DynamicPointcutExample.main(DynamicPointcutExample.java:19)
treeroot 2006-12-12
  • 打赏
  • 举报
回复
错误贴出来看看
sparkcn 2006-12-08
  • 打赏
  • 举报
回复
up,up
treeroot 2006-12-08
  • 打赏
  • 举报
回复
对于类的代理只能使用CGLIB
sparkcn 2006-12-08
  • 打赏
  • 举报
回复
恩,是的,的确是,只能用cglib代理,为什么我的代码不行呢?报错,烦

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧