spring + aspectj,定义一个方面@Around

时间:2010-11-18 00:11:32

标签: java spring aspectj

我想为@Entity

的方法定义一个@Around方面

我的所有实体都在数据包data.entity

定义这样的方面:

@Aspect
public class TestAspect {

    @Around("execution(* data.entity..*(..))")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("INTERCEPT: "+pjp.toLongString());
        return pjp.proceed();
    }
}

但永远不会被截获......我的错误在哪里?

在春季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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:component-scan base-package="data.dao, data.service" />

    <tx:annotation-driven proxy-target-class="true"/>

    <aop:aspectj-autoproxy/>

    <bean id="testAspect" class="spring.TestAspect" />

    ... datasource and other ...

</beans>

我也尝试

@Around("target(data.entity.MyEntity)")

@Around("target(data.entity..)")

但仍无效。

感谢。

2 个答案:

答案 0 :(得分:3)

看起来你使用spring-proxy-aop。仅当类是spring manged bean时才有效,并且必须从其他对象调用adviced方法。

尝试使用真正的aspectJ而不是spring-proxy-aop。

答案 1 :(得分:0)

我刚开始使用AOP,以下是我所在级别的调查结果

  1. 我假设你的应用程序类路径中有必要的jar文件,aspectjweaver-1.6.10.jar和org.springframework.aop-3.0.5.RELEASE.jar。

  2. 您正在定义的方法aroundAdvice是完美的。

  3. 您可以删除以下行并尝试。

    &lt; context:component-scan base-package =“data.dao,data.service”/&gt;