java - @Around 在 Spring AOP 中没有被调用

标签 java spring spring-aop

我正在尝试在 Spring 中使用 AOP 记录一个方法。我仅使用 System.out.println() 尝试了以下代码,但它没有被调用。

已创建注释:

   @Retention(value = RetentionPolicy.RUNTIME)  
   @Target(value = ElementType.METHOD)  
   public @interface Loggable {
   }

创建方面

  @Aspect
  public class MethodLogger  {

     @Around("execution(* *(..)) && @annotation(Loggable)")
     public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("this is called");
        return result;
     }
  }

在服务层的方法中使用日志记录

   @Service("RegionService")
   @Transactional(readOnly = false)
   public class RegionService implements IRegionService{
   @Loggable
   @Override
   public List<> fetch() {      
      return dao.xyz();
   }
   }

Spring 配置

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    >
    <context:component-scan base-package="com.sst.tender.spring"/>
<context:annotation-config/>
<context:spring-configured/>
 </beans>

最佳答案

添加@ComponentMethodLogger类(class)。此外,您还必须通过以下方式之一启用 AspectJ:

  • 添加@EnableAspectJAutoProxy到您的配置 bean 类。 (注释驱动方法)
  • 添加<aop:aspectj-autoproxy />到应用程序上下文文件。 (XML 驱动方法)

关于java - @Around 在 Spring AOP 中没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580854/

相关文章:

java - spring jpa安装程序无法更新数据库

java - 将字节数组放入 JSON,反之亦然

java - 使用@Qualifier注释抛出 "NoUniqueBeanDefinitionException"(发现多个相同类型的bean)

java - Spring MVC的deferredresult如何发送回客户端(Angular JS)

spring - AOP中@within和@annotation有什么区别

java - Spring AOP 中的 Advise 方法错误

java - 为什么@PostConstruct 和@Retryable 不能一起使用?

java - FileChannel 不写任何东西

Java JCombobox 不更改 JPanel 中的图像

java - GWT 无限滚动并丢弃列表开始结果