我有一个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/>
我的问题是我在方面上做错了什么。
答案 0 :(得分:2)
MyAspects
,因为它没有@Component
批注,也没有@Aspect
。 @Repository
确实具有注释。
将@Component
批注添加到MyAspects
,或在XML配置中显式声明Bean:
<bean id="myAspects" class="com.yourpackage.MyAspects">
...
</bean>