我使用 Spring 2.5.6、asm 1.5.3、aspectjrt/aspectjweaver 1.6.1、cglib 2.1_3
在我的基于 Web 的 Spring 应用程序中,我有以下类:
package uk.co.txttools.aspects;
@Aspect
public class LoggingAspect {
@Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))")
public void setLoggingAdvice(){
System.out.println("********************************* Advice run..... set mothod called....");
}
@AfterThrowing("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
public void hadleException(){
System.out.println("================= PreviewMessageController =========== ON SUBMIT Exception Throwen ==================");
}
@Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
public void OnSubmitAspect(){
System.out.println("================= PreviewMessageController =========== ON SUBMIT CALLED ==================");
}
}
我有一个Controller:uk.co.txttools.web.controller.compose.PreviewMessageController
which has
onSubmit()method, which get called from web page.
I have separate
applicationContext.xml` 文件。
My springapp-servlet.xml
(在带有 org.springframework.web.servlet.DispatcherServlet 的 web.xml 文件中使用)文件如下所示:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />
.
.
下面在同一个 xml 文件中PreviewMessageController
得到初始化这意味着我的控制器和方面生活是同一个容器。
我在运行应用程序时没有遇到任何异常,但我的方面类LoggingAspect
永远不会被打电话。
我不确定缺少什么或者我做错了。
请帮我..
Thanks
我不确定我是否做得正确,但对我来说解决它的是添加@Component
到“Aspect'ed”类 -
@Aspect
@Component
public class PerformanceLogger {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Around("within(com.something.rest.service..*)")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object retVal = pjp.proceed();
long end = System.currentTimeMillis();
logger.debug(pjp.getSignature().toShortString() + " Finish: " + (end - start) + "ms");
return retVal;
}
}
(只是为了关闭循环 - 如果您使用基于注释,请不要忘记添加@EnableAspectJAutoProxy
到你的 Config 类。
@EnableAspectJAutoProxy
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)