java - spring + aspectj,定义一个切面@Around

标签 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();
    }
}

但从来没有被拦截...我的错误在哪里?

在 spring 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..)")

但还是不行。

谢谢。

最佳答案

看起来你使用了spring-proxy-aop。仅当该类是 spring 托管 bean 并且建议的方法必须从其他对象调用时才有效。

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

关于java - spring + aspectj,定义一个切面@Around,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210507/

相关文章:

Spring Boot - Hibernate 自定义约束不注入(inject)服务

java - 我们可以根据任何标志的值或通过配置文件启用或禁用方面吗?

java - 提交表单而不转移到目标(Vaadin)

java - 如何在类中创建静态变量,该变量只能由类修改,而不能由实例修改?

java - HTTP 请求正文中的数据如何传递到 servlet

java - 使用 AspectJ 监控 Spring Boot 应用程序中 REST 调用的运行时间

java - 使用 AspectJ 拦截请求范围内的所有 JDBC 调用并作为响应返回

java - 如何在 Java 中从头开始创建 XML 文件?

java - 在 HttpClient 中保留 session

java - 如何从对象列表中选择一个对象