线程“main”中的异常java.lang.ClassCastException:com.sun.proxy。$ Proxy13无法强制转换为CustomeClass

时间:2018-05-26 14:31:14

标签: spring-aop

我正在尝试使用Spring Framework学习AOP,但是有一个异常继续被调用。

  

错误:线程“main”java.lang.ClassCastException中的异常:   com.sun.proxy。$ Proxy13无法强制转换为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"); }
    }
主要方法

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> 

请任何人帮助。

1 个答案:

答案 0 :(得分:0)

感谢MCVE,现在很容易重新创建问题并验证修复。只是阅读代码而无法运行它并没有让我发现问题。

这很简单:您的建议beforeMeth()必须公开。然后一切都像魅力一样。

<强>更新

好的,我想我知道你缺少什么。您正在将创建的bean转换为Triangle,但这不是接口而是类,因此如果没有进一步配置,Spring AOP无法代理它。所以你有两个选择:

  • 您只需将代码更改为Shape tr = (Shape) appContext.getBean("triangle");,以便转换为Spring自动使用的接口,以便创建JDK动态代理。

  • 或者您通过<aop:aspectj-autoproxy proxy-target-class="true"/>使用CBLIB启用类代理。当然,您也可以使用@EnableAspectJAutoProxy(proxyTargetClass = true)

现在这里有一个解决方案,同时显示两种方法。您可以通过更改XML_CONFIG

的值进行切换

顺便说一句,我还将您的包名com.AOP更正为com.aop(小写字母是包的默认设置)和您的方面名称从myCustomAspect更改为MyCustomAspect( Java类应该以大写字符开头。我还将Spring.xml重命名为spring.xml。在tnterfaces中,方法声明不需要public,因为所有接口方法都是公共的。但所有这些只是化妆品,真正的解决方法是上面的那个。

所以这是你改进的代码:

package com.controller;

public interface Shape {
  void draw();
}
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");
  }
}
package com.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyCustomAspect {
  @Pointcut("execution(* *.draw())")
  private void pop() {}

  @Before("pop()")
  public void beforeMeth() {
    System.out.println("this is before draw");
  }
}
package de.scrum_master.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.controller.Shape;
import com.controller.Triangle;

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = { "com.controller", "com.aop" })
public class Application {
  private static final boolean XML_CONFIG = true;
  public static void main(String[] args) {
    ApplicationContext appContext =  XML_CONFIG
      ? new ClassPathXmlApplicationContext("/spring.xml")
      : new AnnotationConfigApplicationContext(Application.class);
    Shape tr = (Triangle) appContext.getBean("triangle");
    tr.draw();
  }
}
<?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:component-scan base-package="com.controller,com.aop"></context:component-scan>
  <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>