AOP AfterReturning 函数 returns 两次
AOP AfterReturning function returns twice
我有个小问题。我在某个函数 returns 之后调用 AfterReturning
函数,而我的 AfterReturning
函数工作了两次,我不想这样。这是代码:
@Aspect
@Component
@Configuration
public class AspectOP {
LogController logcontroller = new LogController();
@AfterReturning("execution(* com..*save*(..))")
public void doSomething() {
logcontroller.guestbook("XD");
}
}
我有 2 个保存功能,我们更改了名称,但还是一样。我试过删除 @Component 或 @Aspect 然后它不起作用。
编辑
我的保存功能
@Controller
@RequestMapping("/api")
public class EntryController {
@Autowired
EntryRepository repo;
Account account;
@RequestMapping(path = "/getir", method = RequestMethod.GET)
public @ResponseBody List<Entries> getir(){
return repo.findAll();
}
@RequestMapping(path = "/saveentry", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody Entries save(@RequestBody Entries entry) {
return repo.save(entry);
}
}
LogController.class
@Controller
public class LogController {
@MessageMapping("/guestbook")
@SendTo("/topic/entries")
public Log guestbook(String message) {
System.out.println("Received message: " + message);
return new Log(message);
}
}
我的主要 objective 是当保存了一些东西时,我将一些东西发送到我的套接字。它正在运行,但 doSomething 函数正在运行两次。
似乎建议也适用于您的 EntryRepository class。将切入点表达式更改为仅应用于 EntryController 的保存方法
@AfterReturning("execution(* com.xyz.EntryController.save*(..))")
例子here
我有个小问题。我在某个函数 returns 之后调用 AfterReturning
函数,而我的 AfterReturning
函数工作了两次,我不想这样。这是代码:
@Aspect
@Component
@Configuration
public class AspectOP {
LogController logcontroller = new LogController();
@AfterReturning("execution(* com..*save*(..))")
public void doSomething() {
logcontroller.guestbook("XD");
}
}
我有 2 个保存功能,我们更改了名称,但还是一样。我试过删除 @Component 或 @Aspect 然后它不起作用。
编辑
我的保存功能
@Controller
@RequestMapping("/api")
public class EntryController {
@Autowired
EntryRepository repo;
Account account;
@RequestMapping(path = "/getir", method = RequestMethod.GET)
public @ResponseBody List<Entries> getir(){
return repo.findAll();
}
@RequestMapping(path = "/saveentry", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody Entries save(@RequestBody Entries entry) {
return repo.save(entry);
}
}
LogController.class
@Controller
public class LogController {
@MessageMapping("/guestbook")
@SendTo("/topic/entries")
public Log guestbook(String message) {
System.out.println("Received message: " + message);
return new Log(message);
}
}
我的主要 objective 是当保存了一些东西时,我将一些东西发送到我的套接字。它正在运行,但 doSomething 函数正在运行两次。
似乎建议也适用于您的 EntryRepository class。将切入点表达式更改为仅应用于 EntryController 的保存方法
@AfterReturning("execution(* com.xyz.EntryController.save*(..))")
例子here