spring - How to solve BeanNotOfRequiredTypeException -
i writing small application check functionality of "afteradvice" concept in spring-aop getting exception related xml file (i think) please me how solve exception
applicationcontext.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>
clientapp.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() 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 less 4 digit throw exception if(pin<=999) throw new illegalargumentexception("invalid pin"); }//afterreturning }//atmpinverifieradvice
if run above application getting exception
output
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 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 of type [com.nt.service.atmpingenerator], of type [com.sun.proxy.$proxy2] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstract beanfactory.java:375) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbe anfactory.java:199) @ org.springframework.context.support.abstractapplicationcontext.getbean(abstracta pplicationcontext.java:962) @ test.clientapp.main(clientapp.java:14)
i don't why getting exception. please 1 me why exception raising?
spring default uses jdk dynamic proxies, interface based, applying aop.
your atmpingenerator
extends random
implements serializable
. spring sees interface , uses interface based proxies. proxy implementing serializable
interface.
you have 3 options solve it
- force class based proxies configuring
proxyfactorybean
- create interface , implement it
- force class based proxies not extending
random
.
the last option easiest don't extend random
, force use of class-based proxies.
public class atmpingenerator { public int pingenerator(){ random rad = new random(); return rad.nextint(9999); } }
you set proxytargetclass
property of proxyfactorybean
true
, don't need change class.
<property name="proxytargetclass" value="true" />
the last option introduce interface , expose pingenerator()
method on , let atmpingenerator
implement interface.
public interface pingenerator { int pingenerator(); } public class atmpingenerator implements pingenerator { ... }
this way using interface based proxied. in main
method use pingenerator
interface instead of atmpingenerator
.
Comments
Post a Comment