【发布时间】:2011-09-29 00:24:41
【问题描述】:
我正在尝试让我的第一个基于 Spring AOP 的 MethodInterceptor 工作,但我的 ultra-simple XML 配置遇到了一些奇怪的异常。
我有一个名为Food 的类,它有一个名为eat(ConsumptionRate rate) 的方法,它将ConsumptionRate 类型的对象作为其唯一参数;每次调用特定方法 (Food::eat(ConsumptionRate)) 时,我都希望 MethodInterceptor 围绕它执行:
public class FoodInterceptor implements MethodInterceptor
{
public Object invoke(MethodInvocation method)
{
try
{
// Do some stuff before target method executes
Object result = method.proceed();
return result;
}
finally
{
// Do some stuff after target method executes.
}
}
}
这是我的 xml 配置 (aop-config.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean name="foodInterceptor" class="com.me.foodproject.core.FoodInterceptor"/>
<aop:config>
<aop:advisor advice-ref="foodInterceptor"
pointcut="execution(public * Food.eat(..))"/>
</aop:config>
项目构建良好(编译等),但是当我运行它时,我收到以下错误:
java.lang.IllegalArgumentException: warning no match for this type name: Food [Xlint:invalidAbsoluteTypeName]
我唯一的猜测是切入点属性中指定的模式在某种程度上是错误的(也许我需要以某种方式在其中包含ConsumptionRate)。但是这个 XML Schema 的文档几乎找不到了,我在这里摸不着头脑。
任何人有任何建议或看到一些让他们跳出来的东西吗?提前致谢!
顺便说一句,如果有人知道记录所有这些<aop> 标记及其属性的网站或任何文献,请告诉我。向我推荐了 Spring AOP 参考的第 6 章,虽然这是一个非常全面的教程,但它只是充满了示例,并没有记录整个 xml 模式。
【问题讨论】:
-
您不是在寻找架构信息,而是在寻找 Spring AOP 文档中介绍的切入点语法。有没有没有包的类
Food? (好吧,对于 this 问题,您需要切入点语法 :) -
嗨@Dave - 感谢您的回复。当你问“有没有没有包装的食物类”时,我能问一下你的意思吗?一切都编译得很好,只是在运行时出现问题。
-
我的意思是“有没有没有包的
Food类?”因为这就是你定义切入点的方式。配置文件不会关心它是否可以正常编译——Spring AOP 发生在执行时,而不是编译时,这与 AspectJ 不同。 -
啊,我没有使用完全限定的包名。谢谢@Dave,如果您将您的回复作为答案,我会为您检查。
标签: java xml spring aop aspectj