1

AOPを実装しようとしましたが(java.lang.ClassCastException) java.lang.ClassCastException: $Proxy0 cannot be cast to myPackage.Person、(2)行で例外が発生します。何が問題なのですか?はPersonスレッドである必要があるため、Threadクラスを拡張します。

主要

1. ApplicationContext ctx = new ClassPathXmlApplicationContext("config/appContext.xml");
2. Person p = (Person)ctx.getBean("p1");
3. p.start(); 

public class Person extends Thread{

    private String name;

    public Person(String name) {
        setPersonName(name);
    }

    public void setPersonName(String name) {
        this.name = name;
    }    
}

側面

public class LogSettersCalls {

    public void logSetterCall(JoinPoint theJoinPoint)
    {
        String methodName = theJoinPoint.getSignature().getName();
        Object newValue = theJoinPoint.getArgs()[0];
        Object theObject = theJoinPoint.getTarget();
        System.out.println(theObject );
    }
}

CONFIG

<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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
       default-lazy-init="true">

    <aop:config>
        <aop:pointcut expression="execution(void set*(*))" id="theSettersLogger" />
        <aop:aspect ref="logSettersCallsBean">
            <aop:before pointcut-ref="theSettersLogger" method="logSetterCall"/>
        </aop:aspect>
    </aop:config>
   <bean id="logSettersCallsBean" class="aop.LogSettersCalls" />


    <bean id="p1" class="myPackage.Person" >    
        <constructor-arg index="0" type="java.lang.String" value="igor"/>
    </bean>
</beans>

df

4

1 に答える 1

1

直感的であるか、派生するのに適しているかどうかわからないという事実は別PersonとしてThread(合成などを使用できませんか?)、問題は、AOPがクラスを書き直しており、それがPerson.

PersonImpl実装 ( ) を適切なインターフェース ( ) から分離するPersonと、キャストが機能するはずです。

于 2012-08-23T08:23:08.027 に答える