Trying to deal with AOP in Spring. Simple advice in @AspectJ is not called. A proxy is created, the aspect information is present in the proxy, but the advice is not invoked. Idea Intellij also recognizes an aspect. I tried both annotations and xml. To date, the code in the annotation:

@Aspect public class TavleiEventPublisher { @Before("execution(* TavleiServerController.beginTurn(..))") public void beginTurn(){ throw new RuntimeException("Begin Turn Advise"); } public TavleiEventPublisher() { } } 

spring.xml file:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy /> <context:component-scan base-package="gamecontrol"/> <bean id="controllerManager" class="gamecontrol.TavleiServerController" init-method="init"/> <bean id="eventListener" class="model.event.EventListener"/> <!-- Aspect for TavleiControllers--> <bean id="tavleiEventPublisher" class="gamecontrol.TavleiEventPublisher"/> </beans> 

pom.xml:

  ... <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring AOP + AspectJ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> ... 

Debug data screenshot Debug picture Screenshot with Intellij Recognition Aspect enter image description here


UPD: The problem was that internal method calls were not caught. Those. beginTurn () was called inside another method (startGame) of the same object. If you call beginTurn () directly from another object, the call is caught.

Alternative solutions are described here: https://stackoverflow.com/questions/5780757/spring-aop-logging-and-nested-methods One solution is to turn on AspectJ to support aop or add self to internal calls ...

Thank!

  • Welcome to Stack Overflow in Russian ! It is better to provide textual information in the form of text, not images: it is more convenient to read, and search engines will index it - aleksandr barakin
  • Thank. But I kind of presented the information in text form. - Irina

1 answer 1

The problem is that aspects and beans are in different contexts. Aspects are found in AplicationContext, and bins (controllers) in WebApplicationContext. AspectJ autoproxy needs to be included in the WebApplicationContext.

  • I have not yet translated into spring MVC. The Aspect Controller was in the same context. - Irina