gpt4 book ai didi

spring - 如何解决 BeanNotOfRequiredTypeException

转载 作者:行者123 更新时间:2023-12-01 02:06:41 27 4
gpt4 key购买 nike

我正在编写一个小应用程序来检查 spring-AOP 中“AfterAdvice”概念的功能,但我得到一个与 xml 文件相关的异常(我认为)请帮助我如何解决异常

应用程序上下文.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="pinCheck" class="com.nt.advice.AtmPinVerifierAdvice" />
<bean id="targetBean" class="com.nt.service.AtmPinGenerator" />
<bean id="pfb" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="targetBean" />
<property name="interceptorNames">
<list>
<value>pinCheck</value>
</list>
</property>
</bean>
</beans>

客户端应用程序.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.nt.service.AtmPinGenerator;
public class ClientApp {
public static void main(String[] args) {
//activate IOC container
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/com/nt/cfgs/applicationContext.xml");
//get proxy obj
AtmPinGenerator gen = ctx.getBean("pfb",AtmPinGenerator.class);
//call b.method
gen.pinGenerator();
}

}

AtmPinGenerator.java
import java.util.Random;
public class AtmPinGenerator extends Random {
//generating pin
public int pinGenerator(){
//creat java.util.Random object
Random rad = new Random();
//use nextInt() to generate random pin of 4 digits
int pin = rad.nextInt(9999);
return pin;
}//pinGenerator
}

AtmPinVerifierAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AtmPinVerifierAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object retValue, Method method, Object[] args,
Object target) throws Throwable {
System.out.println(retValue);
Integer pin = (Integer)retValue;
//if generated pin is less than four digit throw an exception
if(pin<=999)
throw new IllegalArgumentException("invalid pin");
}//afterReturning
}//AtmPinVerifierAdvice

如果我运行上述应用程序,我会收到此异常

输出
Aug 17, 2015 2:03:57 PM     org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@3ff14318: startup date [Mon Aug 17 14:03:57 IST 2015]; root of context hierarchy
Aug 17, 2015 2:03:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [G:\java\Frameworks\SpringAOP\AOPProj6(After advice - pin verifier decl)\src\com\nt\cfgs\applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'pfb' must be of type [com.nt.service.AtmPinGenerator], but was actually of type [com.sun.proxy.$Proxy2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract BeanFactory.java:375)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe anFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractA pplicationContext.java:962)
at test.ClientApp.main(ClientApp.java:14)

我不知道为什么我会得到异常。请任何人帮助我为什么会引发这个异常?

最佳答案

Spring 默认使用基于接口(interface)的 JDK 动态代理来应用 AOP。

您的 AtmPinGenerator扩展 Random实现Serializable . Spring 看到接口(interface)并使用基于接口(interface)的代理。您将获得一个仅实现 Serializable 的代理。界面。

你有 3 个选项来解决它

  • 通过配置 ProxyFactoryBean 强制基于类的代理
  • 创建一个接口(interface)并实现它
  • 通过不扩展 Random 强制基于类的代理.

  • 最后一个选项是最简单的,不要扩展 Random ,这将强制使用基于类的代理。
    public class AtmPinGenerator {

    public int pinGenerator(){
    Random rad = new Random();
    return rad.nextInt(9999);
    }
    }

    您也可以设置 proxyTargetClass ProxyFactoryBean 的属性(property)至 true然后你不需要改变你的类(class)。
    <property name="proxyTargetClass" value="true" />

    最后一个选项是引入一个接口(interface)并简单地公开 pinGenerator()方法就可以了,让你的 AtmPinGenerator实现该接口(interface)。
    public interface PinGenerator {

    int pinGenerator();
    }

    public class AtmPinGenerator implements PinGenerator { ... }

    这样您就可以使用基于接口(interface)的代理。在您的 main方法现在使用 PinGenerator接口(interface)而不是 AtmPinGenerator .

    关于spring - 如何解决 BeanNotOfRequiredTypeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32046182/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com