Пытаюсь использовать в проекте для логгирования аспекты. И возникла следующая довольно странная проблема.
Аспект выполняется не только в том методе, который в pointcut указан, но также - он еще раз выполняется и в том методе, который указанный в pointcut метод вызвал. Т.е. к примеру есть метод:
Java |
1
2
3
4
5
6
7
8
9
| public class Handler {
@LogServiceCall
public ReturnType handle(ArgType message) {
ReturnType response;
//do something
return response;
}
} |
|
Есть класс, из которого этот метод вызывается:
Java |
1
2
3
4
5
6
7
8
9
| public class Caller {
@Autowired
private Handler requestHandler;
public ReturnType getResponse(ArgType message) {
return requestHandler.handle(message);
}
} |
|
При вызове метода getResponse в Caller - аспект вызывается дважды (два раза до, и 2 раза после вызова метода) - сначала в методе handle, а затем еще раз - в методе getResponse.
Код аспекта:
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| @Aspect
public class LoggingAspect {
@Autowired
private ContextLoggerService loggerService;
@Autowired
private RequestContextHolderService contextHolder;
@Around("@annotation(logServiceCall)")
public Object logServiceCall(ProceedingJoinPoint serviceJoinPoint, LogServiceCall logServiceCall) throws Throwable {
//do logging before method call
Object response = serviceJoinPoint.proceed();
//do logging after method call>
return response;
}
} |
|
Аннотация:
Java |
1
2
3
4
5
6
| @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogServiceCall {
ServiceProfile serviceName() default ServiceProfile.NO_SUCH_SERVICE;
} |
|
Конфигурация контекста Spring:
XML |
1
2
3
| <aop:aspectj-autoproxy/>
<bean id="loggingAspect" class="logging.aspect.LoggingAspect"
factory-method="aspectOf"/> |
|
Также на всякий случай привожу конфигурацию mavan-aspectj-plugin:
XML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>8</complianceLevel>
<source>8</source>
<target>8</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<phase>process-test-classes</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/test-classes</weaveDirectory>
</weaveDirectories>
</configuration>
</execution>
</executions>
</plugin> |
|
Помогите разобраться пожалуйста - в чем косяк?