I am trying to learn AOP with Spring Framework, but there is one exception that keeps on getting invoked.
Error : Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to com.controller.Triangle
Shape.java
package com.controller;
public interface Shape {
public void draw();
}
Triangle.java
package com.controller;
import org.springframework.stereotype.Component;
@Component
public class Triangle implements Shape
{
public void draw()
{
System.out.println("this draw method of triangle"),
}
}
myCustomAspect.java
package com.AOP;
import org.aspectj.lang.annotation.After;
@EnableAspectJAutoProxy
@Component
@Aspect
public class myCustomAspect {
@Pointcut("execution(* *.draw())")
private void pop(){}
@Before("pop()")
private void beforeMeth(){
System.out.println("this is before draw"); }
}
inside main method
ApplicationContext ssp = new ClassPathXmlApplicationContext("/Spring.xml");
Shape tr=(Triangle)ssp.getBean("triangle");
tr.draw();
Spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.controller,com.AOP"></context:component-scan>
</beans>
Please any one help.
Shape. Also learn what an MCVE is and how it helps you to get good answers quickly here.