Spring AOP(方面)未执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14461168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring AOP (Aspect) Not executing
提问by bhavin
I ams using Spring 2.5.6, asm 1.5.3, aspectjrt/aspectjweaver 1.6.1, cglib 2.1_3 In my Web based Spring application I have following class:
我正在使用 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 ==================");
}
}
I have one Controller:uk.co.txttools.web.controller.compose.PreviewMessageController
which hasonSubmit()method, which get called from web page.
I have separateapplicationContext.xml` file.
我有一个Controller:uk.co.txttools.web.controller.compose.PreviewMessageController
which hasonSubmit() method, which get called from web page.
I have separateapplicationContext.xml` 文件。
My springapp-servlet.xml(which is used in web.xml file with org.springframework.web.servlet.DispatcherServlet) file looks like this:
我的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" />
.
.
Below in same xml file PreviewMessageControllerget initialize which means my Controller and Aspect live is same container.
下面在同一个 xml 文件中PreviewMessageControllerget initialize 这意味着我的 Controller 和 Aspect live 是同一个容器。
I don't get any exception while running application but my aspect class LoggingAspectnever get called.
I'm not sure what is that missing or I'm doing wrong.
Please help me..
运行应用程序时我没有遇到任何异常,但我的方面类LoggingAspect从未被调用。我不确定缺少什么或我做错了什么。请帮我..
Thanks
谢谢
采纳答案by bhavin
Finally SOLVED it.
终于解决了。
I think I was missing aspectj-maven-plugin. It required for spring to weaving of aspects. None tutorial provide this information though. Added following to my pom.xml.
我想我错过了 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>
Thanks guys
谢谢你们
回答by baraka
I'm not sure if I did it properly but for me what solved it was adding @Componentto the "Aspect'ed" class -
我不确定我是否做得正确,但对我来说,解决它的方法是将其添加@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;
}
}
(And just to close the loop - if you are using annotation based, don't forget adding @EnableAspectJAutoProxyto your Config class.
(并且只是为了关闭循环 - 如果您使用基于注释,请不要忘记添加@EnableAspectJAutoProxy到您的 Config 类。
@EnableAspectJAutoProxy
回答by Zakaria
For those who opted for JavaConfig, you can declare your Aspectas a bean and add the @EnableAspectJAutoProxyannotation to turn on auto-proxying :
对于那些选择JavaConfig,您可以将您的声明声明Aspect为 bean 并添加@EnableAspectJAutoProxy注释以打开自动代理:
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class MyConfig {
@Bean
public LoggingAspect loggingAspect(){
return new LoggingAspect();
}
}
回答by PowerStat
Only to make the list of possible answers complete:
只是为了使可能的答案列表完整:
To me it looks like you are missing the following dependency in your maven pom.xml:
在我看来,您的 maven pom.xml 中缺少以下依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.5.6</version>
</dependency>
回答by Vikram
If you havent tried already...try the xml based spring-aop configuration as follows:
如果您还没有尝试过……请尝试使用基于 xml 的 spring-aop 配置,如下所示:
<aop:config>
<aop:aspect ref="loggingAspect">
<aop:pointcut expression="execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))" id="previewMessageControllerSetters"/>
<aop:before method="setLoggingAdvice" pointcut-ref="previewMessageControllerSetters"/>
// set other 2 pointcuts similarly....
</aop:aspect>
</aop:config>
<bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />
回答by Vikram
Try this in your config file:
在你的配置文件中试试这个:
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="loggingAspect"/>
</aop:aspectj-autoproxy>

