Java 弹簧&x2B;aspectj,在周围定义一个方面

Java 弹簧&x2B;aspectj,在周围定义一个方面,java,spring,aspectj,Java,Spring,Aspectj,我想为@Entity的方法定义一个@Around方面 我的所有实体都在包data.entity中 A定义如下方面: @Aspect public class TestAspect { @Around("execution(* data.entity..*(..))") public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable { System.out.println("INTERCE

我想为@Entity的方法定义一个@Around方面

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

A定义如下方面:

@Aspect
public class TestAspect {

    @Around("execution(* data.entity..*(..))")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("INTERCEPT: "+pjp.toLongString());
        return pjp.proceed();
    }
}
但永远不会被拦截。。。我的错误在哪里

在SpringXML中,我有以下内容:

<?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>

但仍然不起作用


谢谢。

看起来您使用的是spring代理aop。只有当类是spring管理的bean,并且adviced方法必须从其他对象调用时,这才有效


尝试使用RealAspectJ而不是spring代理aop。

我刚刚开始使用aop,下面是我级别的发现

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

  • 正如您目前定义的那样,Advice周围的方法是完美的

  • 你能不能把下面的线去掉,再试一次

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