Я хочу использовать Spring AOP для некоторых вещей в моем приложении. Я уже использовал AOP в отдельном приложении, и оно работало, но теперь с веб-приложением на коте оно не работает.
У меня есть приложение-ядро и приложение-веб-проект, где в основном проекте происходит вся логика, а веб-проект содержит только веб-материалы.
Сначала я попытался добавить свой класс LoggingAspect в свой основной проект, так как это не сработало, но теперь я переместил его в веб-проект, но там он не работает.
Вот мое applicationContext.xml, которое находится в папке: / application-web / src / main / webapp
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Add AspectJ autoproxy support for AOP -->
<aop:aspectj-autoproxy/>
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="my.foobar"/>
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
И это мой класс LoggingAspect, который сейчас находится здесь: application-web / src / main / my.foobar.web / aspect / LoggingAspect
@Aspect
@Order(1)
public class LoggingAspect {
private static final LogHandler LOG = LogHandler.getLogger(LoggingAspect.class);
@Pointcut("execution(* my.foobar.*.*.*(..))")
private void completePackage(){}
@Pointcut("execution(* my.foobar.*.*.get*(..))")
private void getterMethods(){}
@Pointcut("execution(* my.foobar.*.*.set*(..))")
private void setterMethods(){}
@Pointcut("completePackage() && !(getterMethods() || setterMethods())")
private void allMethodsExceptGetterAndSetter(){}
@Around("completePackage()")
private Object aroundMethod(ProceedingJoinPoint theProceedingJointPoint) throws Throwable{
LOG.info("around method");
String method = theProceedingJointPoint.getSignature().getName();
Object[] arguments = theProceedingJointPoint.getArgs();
LOG.info("method call: {0}", method);
for(Object arg: arguments){
LOG.info("argument[{0}]", arg);
}
Object result = theProceedingJointPoint.proceed();
return result;
}
}
Я также добавил класс в свой веб-проект приложения
@Configurable
@EnableAspectJAutoProxy
@ComponentScan("my.foobar")
public class ApplicationWebAppConfig {
}
Я ожидаю, что каждый вызываемый метод будет регистрироваться, но этого не происходит.