2016-08-18 601 views
3

我有一点问题。我在函数返回后调用AfterReturning函数,我的AfterReturning函数工作了两次,我不想这样做。这里是代码:AOP 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); 
    } 
} 

我的主要目标是当事情被保存,我送点东西给我的插座。它正在工作,但是某些功能正在工作两次。

+0

也可以显示您所期望的忠告适用于 – kuhajeyan

+0

我添加类的类。 –

回答

0

似乎建议也适用于您的EntryRepository类。改变你的切入点表达类似,将仅适用于EntryController的保存方法

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

例子here

+0

但我希望这个函数可以在函数的名字中有“保存”字的任何类中运行。 –

+0

@Gürsel因此,正如预期的那样发生(使用您的切入点),当您调用EntryController.save时,建议将会应用两次, 1. EntryController.save(..) 2. EntryRepository.save(..) - 因为它是在EntryController.save中调用的 – kuhajeyan

+0

EnrtyRepository是emty。它没有任何功能。 –