java - i am trying to implement a common interface using two bean class? -
recently practicing spring aop. solving above issue trying use aop " introductions - adding functionality aspect" concepts in code using 2 bean class called "blender & fan" 2 bean implement common interface name "imachine.java" interface implement class name "machinewear.java" when run app.java class getting below exception.how can solve it??
may 09, 2016 2:11:27 pm org.springframework.context.support.classpathxmlapplicationcontext preparerefresh info: refreshing org.springframework.context.support.classpathxmlapplicationcontext@c4437c4: startup date [mon may 09 14:11:27 bdt 2016]; root of context hierarchy may 09, 2016 2:11:28 pm org.springframework.beans.factory.xml.xmlbeandefinitionreader loadbeandefinitions info: loading xml bean definitions class path resource [com/mir00r/spring/aop/beans.xml]
may 09, 2016 2:11:29 pm org.springframework.beans.factory.support.defaultlistablebeanfactory preinstantiatesingletons info: pre-instantiating singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@66a3ffec: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,blender,camera,car,fan,logger,machineaspects,lens,org.springframework.aop.config.internalautoproxycreator,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor]; root of factory hierarchy
exception in thread "main" java.lang.classcastexception: com.sun.proxy.$proxy9 cannot cast com.mir00r.spring.aop.imachine @ com.mir00r.spring.aop.app.main(app.java:44)
hare source code
/springaop/src/com/mir00r/spring/aop/app.java
package com.mir00r.spring.aop; import org.springframework.context.support.classpathxmlapplicationcontext; public class app { public static void main(string[] args) { classpathxmlapplicationcontext context = new classpathxmlapplicationcontext("com/mir00r/spring/aop/beans.xml"); iblender blender = (iblender)context.getbean("blender"); ( (imachine) blender).start(); blender.blend(); ifan fan = (ifan) context.getbean("fan"); ( (imachine) fan).start(); fan.active(3); context.close(); } }
/springaop/src/com/mir00r/spring/aop/machineaspects.java
package com.mir00r.spring.aop; import org.aspectj.lang.proceedingjoinpoint; import org.aspectj.lang.annotation.around; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.declareparents; import org.springframework.stereotype.component; @component @aspect public class machineaspects { @declareparents( value = "com.mir00r,spring.aop.*", defaultimpl = com.mir00r.spring.aop.machinewear.class ) private imachine machine; @around("within(com.mir00r.spring.aop.*)") public void runmachine( proceedingjoinpoint jp ){ system.out.println("running........"); try { jp.proceed(); } catch (throwable e) { e.printstacktrace(); } system.out.println(".....completed"); } }
/springaop/src/com/mir00r/spring/aop/beans.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.mir00r.spring.aop"></context:component-scan> <aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy> </beans>
/springaop/src/com/mir00r/spring/aop/blender.java
package com.mir00r.spring.aop; import org.springframework.stereotype.component; @component public class blender implements iblender { /* (non-javadoc) * @see com.mir00r.spring.aop.iblender#blend() */ @override public void blend (){ system.out.println("blending........"); } }
/springaop/src/com/mir00r/spring/aop/iblender.java
package com.mir00r.spring.aop; public interface iblender { public abstract void blend(); }
/springaop/src/com/mir00r/spring/aop/fan.java
package com.mir00r.spring.aop; import org.springframework.stereotype.component; @component public class fan implements ifan { /* (non-javadoc) * @see com.mir00r.spring.aop.ifan#active(int) */ @override public void active ( int level ){ system.out.println("fan running level is: " + level); } }
/springaop/src/com/mir00r/spring/aop/ifan.java
package com.mir00r.spring.aop; public interface ifan { public abstract void active(int level); }
/springaop/src/com/mir00r/spring/aop/machinewear.java
package com.mir00r.spring.aop; public class machinewear implements imachine { /* (non-javadoc) * @see com.mir00r.spring.aop.imachine#star() */ @override public void start (){ system.out.println("machine starting........"); } }
/springaop/src/com/mir00r/spring/aop/imachine.java
package com.mir00r.spring.aop; public interface imachine { public void start(); }
Comments
Post a Comment