Spring AspectJ 建议未执行
Posted
技术标签:
【中文标题】Spring AspectJ 建议未执行【英文标题】:Spring AspectJ advice not executing 【发布时间】:2014-10-01 11:32:00 【问题描述】:我试图在执行 Action 之前执行 Logging 建议,但该操作被调用但 Advice 没有执行。 我正在使用 JSF 2.2、Spring 3.2、AspectJ-1.6.11 请让我知道我做错了什么,因为它没有给出任何错误,只是建议没有执行。
下面是代码示例
LoggingAspect.java
package com.igate.mldb.util.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggingAspect
@Around("com.example.action.FirstAction.checkLogin()")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable
System.out.println("logAround() is running!");
System.out.println("hijacked method : " + joinPoint.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));
System.out.println("Around before is running!");
joinPoint.proceed(); //continue on the intercepted method
System.out.println("Around after is running!");
System.out.println("******");
@Pointcut("execution(public * *(..))")
public void anyPublicOperation()
System.out.println("Inside Pointcut execution");
@Before("execution(public * *(..))")
public void logBefore(JoinPoint joinPoint)
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
动作类 FirstAction.java
package com.example.action;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("request")
public class FirstAction
private String name;
private String password;
public String checkLogin()
System.out.println("In Action");
if ((name.equals("mks")) && (password.equals("mks")))
System.out.println("In IF");
System.out.println("Name -->" + name + " password-->" + password);
return "SUCCESS";
else
System.out.println("In else");
System.out.println("Name -->" + name + " password-->" + password);
return "error";
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
下面是我的spring配置 ApplicationContext.xml
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.example.action, com.igate.mldb" />
JSF 配置 faces-config.xml
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<resource-bundle>
<base-name>MLDB</base-name>
<var>mldb</var>
</resource-bundle>
</application>
问候, Swapneel Killekar
【问题讨论】:
【参考方案1】:我对 Spring 了解不多,但我知道 AspectJ 语法,并且可以告诉您,您的 @Around
建议的切入点在方法签名中缺少返回类型。应该是这样的:
@Around("* com.example.action.FirstAction.checkLogin()")
或者,更具体地说,因为checkLogin
返回一个String
:
@Around("String com.example.action.FirstAction.checkLogin()")
因此,您的环绕建议不能返回void
,它也需要返回Object
或String
:
@Around("String com.example.action.FirstAction.checkLogin()")
public String logAround(ProceedingJoinPoint joinPoint) throws Throwable
System.out.println("logAround() is running!");
System.out.println("hijacked method : " + joinPoint.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));
System.out.println("Around before is running!");
String result = joinPoint.proceed(); //continue on the intercepted method
System.out.println("Around after is running!");
System.out.println("******");
return result;
我不知道你的 Spring 配置是否正确,我只能帮助 AspectJ 部分,抱歉。
【讨论】:
是的,我想通了...无论如何感谢您的回答。以上是关于Spring AspectJ 建议未执行的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect 报这个错 但是我又找不到aspec
生命周期配置未涵盖插件执行:org.codehaus.mojo:aspectj-maven-plugin:1.0
Spring AOP Capabilities and Goals