Spring 引导 1.5.12 方面未调用
Spring boot 1.5.12 Aspect not called
我知道 Whosebug 中有很多类似的问题,但其中 none 帮助了我。
我有这样的控制器:
com.mypkg.controller;
@RestController
public class MyController {
@RequestMapping(method = RequestMethod.POST,
......
public ResponseEntity<?> MyEndpoint(myParams) {
return this.myMethod(myParams, "myString");
}
public ResponseEntity<?> myMethod(myParams, String myString){
//do something
return myReponseEntity
}
}
我是这样定义我的方面的:
com.mypkg.controller;
@Aspect
@Component
@Slf4j
public class MyAspect {
@Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..)) && args(..,aParam)")
public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
// I am working fine
// do something
return
}
@Around("execution(* com.mypkg.controller.MyController.myMethod(..)) && args(..,myString)")
public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
// **** I AM NOT CALLED****
// do something
// return ...
}
}
我配置了 AutoProxy
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AopConfig {}
每次调用 MyEndpoint(抛出 REST api)时都会调用函数 endpointAround
。
问题是第二个@Around
。它没有被调用。每次 MyEndpoint
执行时我都需要调用一个方法,而另一个 MyEndpoint
调用 myMethod
.
的方法
问题是您的方法 myMethod
是从您的其他方法中直接调用的,而不是作为 someSpringBean.myMethod
。
spring 的工作方式是包装任何 bean,然后在 'wrapping' 上它可以执行所有方面或其他 spring 相关的东西。当您从同一个 class 中的另一个方法调用一个方法时,您不会进行包装,因此不会发生方面相关的事情
您只是漏掉了一些代码。让我们使用下面的代码片段,它将起作用。
使用此 com.mypkg.controller.MyController.myMethod
而不是
com.mypkg.controller.myMethod
它会起作用
控制器
com.mypkg.controller;
@RestController
public class MyController {
@RequestMapping(method = RequestMethod.POST,
......
public ResponseEntity<?> MyEndpoint(myParams) {
return this.myMethod(myParams, "myString");
}
public ResponseEntity<?> myMethod(myParams, String myString){
//do something
return myReponseEntity
}
}
和看点
com.mypkg.controller;
@Aspect
@Component
@Slf4j
public class MyAspect {
@Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..)) && args(..,aParam)")
public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
// I am working fine
// do something
return
}
@Around("execution(* com.mypkg.controller.MyController.myMethod(..)) && args(..,myString)")
public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
// **** I AM NOT CALLED****
// do something
// return ...
}
}
您只是错过了包路径。 你的方法路径应该是这样的...springbean.method
我知道 Whosebug 中有很多类似的问题,但其中 none 帮助了我。
我有这样的控制器:
com.mypkg.controller;
@RestController
public class MyController {
@RequestMapping(method = RequestMethod.POST,
......
public ResponseEntity<?> MyEndpoint(myParams) {
return this.myMethod(myParams, "myString");
}
public ResponseEntity<?> myMethod(myParams, String myString){
//do something
return myReponseEntity
}
}
我是这样定义我的方面的:
com.mypkg.controller;
@Aspect
@Component
@Slf4j
public class MyAspect {
@Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..)) && args(..,aParam)")
public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
// I am working fine
// do something
return
}
@Around("execution(* com.mypkg.controller.MyController.myMethod(..)) && args(..,myString)")
public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
// **** I AM NOT CALLED****
// do something
// return ...
}
}
我配置了 AutoProxy
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AopConfig {}
每次调用 MyEndpoint(抛出 REST api)时都会调用函数 endpointAround
。
问题是第二个@Around
。它没有被调用。每次 MyEndpoint
执行时我都需要调用一个方法,而另一个 MyEndpoint
调用 myMethod
.
问题是您的方法 myMethod
是从您的其他方法中直接调用的,而不是作为 someSpringBean.myMethod
。
spring 的工作方式是包装任何 bean,然后在 'wrapping' 上它可以执行所有方面或其他 spring 相关的东西。当您从同一个 class 中的另一个方法调用一个方法时,您不会进行包装,因此不会发生方面相关的事情
您只是漏掉了一些代码。让我们使用下面的代码片段,它将起作用。
使用此 com.mypkg.controller.MyController.myMethod
而不是
com.mypkg.controller.myMethod
它会起作用
控制器
com.mypkg.controller;
@RestController
public class MyController {
@RequestMapping(method = RequestMethod.POST,
......
public ResponseEntity<?> MyEndpoint(myParams) {
return this.myMethod(myParams, "myString");
}
public ResponseEntity<?> myMethod(myParams, String myString){
//do something
return myReponseEntity
}
}
和看点
com.mypkg.controller;
@Aspect
@Component
@Slf4j
public class MyAspect {
@Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..)) && args(..,aParam)")
public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
// I am working fine
// do something
return
}
@Around("execution(* com.mypkg.controller.MyController.myMethod(..)) && args(..,myString)")
public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
// **** I AM NOT CALLED****
// do something
// return ...
}
}
您只是错过了包路径。 你的方法路径应该是这样的...springbean.method