java - ClassCastException $Proxy 无法转换为使用 aop

标签 java spring aop

我正在使用 spring 通过 bean 创建对象。现在我尝试使用 aop 创建相同的对象,但我得到 $Proxy cannot be cast to SaleRoom 异常。

之前的 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>

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

    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 日志记录类:

@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);
    }
}

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

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

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

最佳答案

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

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

因为如果您的 bean 实现了某个接口(interface),那么默认情况下 Spring 将基于该接口(interface)创建代理。

这里是 a good article about proxy creation in Spring.

如果你想为目标类创建代理,你也可以改变 Spring AOP 的代理机制。这是描述here in reference documentation .

关于java - ClassCastException $Proxy 无法转换为使用 aop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461985/

相关文章:

java - Java动态代理的性能成本

java - Hibernate Criteria 将两个表与第二个表上的条件连接起来,结果是第一个表

java - 为什么我们不能在(非静态)内部类(Java 16 之前)中有静态方法?

java 将 unicode 字符打印到 bash shell (mac OsX)

java - 拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop

java - Weblogic 中的 EJB 拦截器

programming-languages - 哪些框架(和相关语言)支持类替换?

java - 链接 POM 版本和应用程序版本

java - 在 Spring 4 中运行并行线程的优雅方式

java - 找不到类 Spring MVC