Spring AOP-@AfterReturning不起作用

问题描述 投票:1回答:1

我有一个Java方法getAllItems(),并创建了一个Aspect方法,该方法在getAllItems()结束后被调用。

@Repository
@Scope("prototype")
public class ItemDao {
    // not using database connection for this question; the program works fine, except aspects
    public List<String> getIAllItems() {
        List<String> items = new ArrayList();
        items.add("item1");
        return items;
    }
}

方面类是这里:

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import java.util.List;

@Aspect
public class MyAspects {

    @AfterReturning(pointcut = "execution(* ro.telacad.dao.ItemDao.getIAllItems(..))", returning="items")
    public void afterGetAllItems(List<String> items) {
        System.out.println(items);
    }
}

因此,在调用getAllItems()之后,我希望在控制台中看到打印的“ [item1]”,但是未调用Aspect方法。控制台中没有错误,该应用程序除各方面外均正常运行。因此,我认为所有弹簧豆都已创建。

在appConfig.xml中,bean的声明如下:

<context:component-scan base-package="ro.telacad.*" />
<aop:aspectj-autoproxy/>

我的问题是我在方面上做错了什么。

spring spring-aop spring-aspects
1个回答
1
投票

MyAspects没有被Spring的组件扫描拾取,因为它没有@Component批注,也没有@Aspect@Aspect确实具有注释。

@Repository批注添加到@Repository,或在XML配置中显式声明该bean:

@Component
© www.soinside.com 2019 - 2024. All rights reserved.