我尝试实现 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 );
}
}
配置
<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