我有我们的方面,注释和& MVC控制器编写如下:
方面
@Aspect
public class AuditAspect {
@Around(value = "@annotation(com.test.Audit)")
public Object audit(ProceedingJoinPoint pjp) {
System.out.println("Inside the Audit aspect ...");
Object result = null;
try {
result = pjp.proceed();
} catch (Throwable t) {
}
return result;
}
}
注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Audit
{
AuditType auditType();
}
控制器:
@RestController
@RequestMapping("/patients")
public class PatientController {
@Audit(auditType = AuditType.PATIENT_LIST)
@RequestMapping(value="", method=RequestMethod.GET)
public APIResponse getPatients(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false, value="audit") String sAudit) {
System.out.println("Inside getPatients ...");
return null;
}
}
但是,每当我发出休息请求时,方面的审核方法都不会被调用。
寻找一些帮助。发现很少有帖子提到AspectJ没有使用Spring MVC控制器。但是,我尝试使用一个简单的spring MVC应用程序,并且即使控制器方法被注释,方面也被正确调用。不知道这里出了什么问题。这里的任何指针/建议都会非常有用。
我试过的示例应用程序没有使用spring事务管理器,或者与hibernate等集成......这会有什么不同吗?
另外,下面给出了上下文文件条目:
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test">
<context:include-filter type="aspectj" expression="com.test.AuditAspect" />
</context:component-scan>
<context:annotation-config />
答案 0 :(得分:2)
为了使Spring AOP正常工作,您的方面和目标对象都必须是Spring @Component
。