gpt4 book ai didi

java - AOP AfterReturning 函数返回两次

转载 作者:行者123 更新时间:2023-12-03 21:35:17 26 4
gpt4 key购买 nike

我有个小问题。我在某些函数返回后调用 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);
}
}

我的主要目标是当保存了一些东西时,我将一些东西发送到我的套接字。它正在运行,但 doSomething 函数正在运行两次。

最佳答案

似乎建议也适用于您的 EntryRepository 类。将切入点表达式更改为仅应用于 EntryController 的保存方法

@AfterReturning("execution(* com.xyz.EntryController.save*(..))")

示例 here

关于java - AOP AfterReturning 函数返回两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com