我写了简单的Spring应用程序 使用xml中的配置:
<bean id="flyingDelegate" class="pl.marcindebski.spring.aspects.LowFlyingKnight"></bean>
<aop:config>
<aop:aspect ref="usageTracking">
<aop:declare-parents
types-matching="pl.marcindebski.spring.aspects.Knight+"
implement-interface="pl.marcindebski.spring.aspects.FlyingKnight"
delegate-ref="flyingDelegate"
/>
</aop:aspect>
</aop:config>
当我使用代码运行app时:
Knight knight = context.getBean(Knight.class);
if (knight instanceof FlyingKnight)
{
FlyingKnight flyingKnight =(FlyingKnight)knight;
flyingKnight.fly();
}
context.close();
在我flyingKnight.fly();
的行上:
Exception in thread "main" org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract void pl.marcindebski.spring.aspects.FlyingKnight.fly()] on target [pl.marcindebski.spring.aspects.BraveKnight@5e82df6a]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
问题:
1)类LowFlyingKnight
从fly()
接口实现FlyingKnight
方法。为什么Spring认为Knight
FlyingKnight
(knight instanceof FlyingKnight
)是正确的,但是在调用fly()
方法时失败了。
2)BraveKnight类是Knight接口的一个实现,所以为什么knight instanceof BraveKnight
为false,但是knight.embarkOnQuest
从BraveKnight执行实现
主要课程 - &gt; App.java
package pl.marcindebski.spring.aspects;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) throws Exception {
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
new String[] {
"E:/dev/eclipse-workspaces/1-aspects/flying.xml",
"E:/dev/eclipse-workspaces/1-aspects/minstrel.xml" });
Knight braveKnight = context.getBean(Knight.class);
if (braveKnight instanceof BraveKnight) { // FALSE!
braveKnight.embarkOnQuest();
}
braveKnight.embarkOnQuest(); // Working!
if (braveKnight instanceof FlyingKnight) // true
{
FlyingKnight flyingKnight = (FlyingKnight) braveKnight;
flyingKnight.fly();// why its not working ?
}
context.close();
}
}
方面:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="flyingDelegate" class="pl.marcindebski.spring.aspects.LowFlyingKnight"></bean>
<aop:config>
<aop:aspect>
<aop:declare-parents
types-matching="pl.marcindebski.spring.aspects.Knight+"
implement-interface="pl.marcindebski.spring.aspects.FlyingKnight"
delegate-ref="flyingDelegate"
/>
</aop:aspect>
</aop:config>
</beans>
bean定义:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="knight" class="pl.marcindebski.spring.aspects.BraveKnight">
<constructor-arg ref="quest" />
</bean>
<bean id="quest" class="pl.marcindebski.spring.aspects.SlayDragonQuest">
<constructor-arg value="#{T(System).out}" />
</bean>
<bean id="minstrel" class="pl.marcindebski.spring.aspects.Minstrel">
<constructor-arg value="#{T(System).out}" />
</bean>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark"
expression="execution(* *.embarkOnQuest(..))"/>
<aop:before pointcut-ref="embark"
method="singBeforeQuest"/>
<aop:after pointcut-ref="embark"
method="singAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>