我正在尝试在 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>
最佳答案
添加@Component
至MethodLogger
类(class)。此外,您还必须通过以下方式之一启用 AspectJ:
- 添加
@EnableAspectJAutoProxy
到您的配置 bean 类。 (注释驱动方法) - 添加
<aop:aspectj-autoproxy />
到应用程序上下文文件。 (XML 驱动方法)
关于java - @Around 在 Spring AOP 中没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580854/