春天 + spectj,定义一个切面@Around

发布于 2024-10-03 09:41:05 字数 1765 浏览 0 评论 0原文

我想为我的 @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();
    }
}

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

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

仍然不起作用。

谢谢。

i want to define an @Around aspect for a method of my @Entity

All my entities are in package data.entity

A define an aspect like this:

@Aspect
public class TestAspect {

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

But never is intercepted... where is my error?

In spring xml i have this:

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

I try also

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

and

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

but still not work.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-10-10 09:41:05

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

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

It looks like you use spring-proxy-aop. This works only if the class is a spring manged bean, and the adviced method must be invoked from an other object.

Try to use real aspectJ instead of spring-proxy-aop.

晒暮凉 2024-10-10 09:41:05

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

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

  2. 您当前定义的 aroundAdvice 方法是完美的。

  3. 您可以删除下面的行并尝试吗?

    >

I have just started using AOP and below are the findings at my level

  1. i am assuming you have necessary jar files, aspectjweaver-1.6.10.jar and org.springframework.aop-3.0.5.RELEASE.jar present in your apps classpath.

  2. The method aroundAdvice, as you have defined currently is perfect.

  3. Could you remove the below line and try.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文