ClassCastException $ Proxy无法使用aop强制转换为 [英] ClassCastException $Proxy cannot be cast to using aop

查看:160
本文介绍了ClassCastException $ Proxy无法使用aop强制转换为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用spring通过bean创建对象。现在我尝试使用aop创建相同的对象,我得到$ Proxy无法转换为SaleRoom异常。

I was using spring to create objects through beans. Now I tried to use aop to create the same object and I get $Proxy cannot be cast to SaleRoom exception.

之前的xml是:

<?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/spring-aop-2.5.xsd"
xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config- 1.0.xsd"
xmlns:jm s="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd     http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd     http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd
http://www.springframework.org/schema/jms/spring-jms-2.5.xsd     http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd     http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd     http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/osgi/spring-osgi.xsd     http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/util/spring-util-2.5.xsd     http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd
">
<bean id="sale01" class="application.common.entities.BidRoom">
<property name="itemId" value="0001"/>
<property name="lifeTime" value="15"/>
</bean>
</beans>

我使用以下代码创建销售:

And I used the following code to create the sales:

    ApplicationContext context = new FileSystemXmlApplicationContext(SalesManager.getSalesSourceFile());
    SaleRoom saleRoom;
    List<String> salesNames = new LinkedList<String>();
    List<SaleRoom> allSales = new LinkedList<SaleRoom>();

    // Get all sales id's for beans
    NodeList salesNodeList = salesDoc.getElementsByTagName("bean");

    for (int i = 0; i < salesNodeList.getLength(); i++) {
        Node nNode = salesNodeList.item(i);
        salesNames.add(((Element) nNode).getAttribute("id").toString());
    }

    for (String saleName : salesNames) {
        if(saleName.contains("sale")) {
            saleRoom = (SaleRoom) context.getBean(saleName);
            allSales.add(saleRoom);
        }
    }

    return allSales;

这是新的xml:

<?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.0.xsd">

    <aop:aspectj-autoproxy>
        <aop:include name="logSettersCalls"/>
    </aop:aspectj-autoproxy>
    <bean id="logSettersCalls" class="application.logging.aop.LogSettersCalls"/>

    <bean id="sale01" class="application.common.entities.BidRoom">
        <constructor-arg index="0" type="int" value="0001"/>
        <constructor-arg index="1" type="int" value="15"/>
    </bean>
</beans>

Aspect日志记录类:

The Aspect logging class:

@Aspect
public class LogSettersCalls {
   @Pointcut("execution(void set*(*))")
    public void setMethod() {}

    @Before("setMethod()")
    public void logSetterCall(JoinPoint theJoinPoint) {
        String methodName = theJoinPoint.getSignature().getName();
        Object newValue = theJoinPoint.getArgs()[0];
        Object theObject = theJoinPoint.getTarget();
        System.out.println("The method " + methodName + " is called on object " 
                + theObject + " with the value " + newValue);
    }
}

我使用相同的代码创建bean通过aop。我得到
线程main中的异常java.lang.ClassCastException:$ Proxy11无法强制转换为application.common.entities.SaleRoom

I'm using the same code for creating the beans via aop. and I get Exception in thread "main" java.lang.ClassCastException: $Proxy11 cannot be cast to application.common.entities.SaleRoom

该行抛出异常:
saleRoom =(SaleRoom)context.getBean(saleName);

The line that throws the exception: saleRoom = (SaleRoom) context.getBean(saleName);

任何帮助将不胜感激。谢谢。

Any help will be appreciated. Thanks.

推荐答案

您的SaleRoom类是否实现了某些界面?如果是,那么您应该在代码中使用接口而不是类:

Does your SaleRoom class implement some interface? If yes, then you should use interface and not class in you code:

ISaleRoom saleRoom = (ISaleRoom) context.getBean(saleName);

因为如果你的bean实现了一些接口,那么Spring默认会根据这个接口创建代理。

Because if your bean implements some interface then Spring by default will create proxy based on this interface.

这是一个好的有关Spring中代理创建的文章。

如果要为目标类创建代理,也可以更改Spring AOP的代理机制。 此处参考文档中对此进行了描述。

Also you can change proxying mechanism for Spring AOP if you want to create proxy for target class. This is described here in reference documentation.

这篇关于ClassCastException $ Proxy无法使用aop强制转换为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆