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