bha*_*vin 17 aop spring spring-mvc spring-aop
我使用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 ==================");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个Controller:uk.co.txttools.web.controller.compose.PreviewMessageController
which hasonSubmit()method, which get called from web page.
I have separateapplicationContext.xml`文件.
我的springapp-servlet.xml(在web.xml文件中使用org.springframework.web.servlet.DispatcherServlet)文件如下所示:
<?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" />
.
.
Run Code Online (Sandbox Code Playgroud)
下面在同一个xml文件中PreviewMessageController获取初始化,这意味着我的Controller和Aspect live是同一个容器.
我在运行应用程序时没有遇到任何异常,但我的方程类LoggingAspect永远不会被调用.我不确定那是什么遗失或我做错了.请帮我..
谢谢
小智 28
我不确定我是否做得不错,但对我来说解决它的是加入@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;
}
}
Run Code Online (Sandbox Code Playgroud)
(并且只是关闭循环 - 如果您使用基于注释,请不要忘记添加@EnableAspectJAutoProxy到您的Config类.
@EnableAspectJAutoProxy
Run Code Online (Sandbox Code Playgroud)
终于解决了.
我想我缺少aspectj-maven-plugin.它需要春天编织方面.没有教程提供此信息.在我的pom.xml中添加了以下内容.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
多谢你们
对于那些选择的人JavaConfig,您可以将your声明Aspect为bean并添加@EnableAspectJAutoProxy注释以启用自动代理:
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class MyConfig {
@Bean
public LoggingAspect loggingAspect(){
return new LoggingAspect();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24406 次 |
| 最近记录: |