Issue
I am trying to execute a Logging advice prior to execute of Action, but the action gets called but the Advice is not executing. I am using JSF 2.2, Spring 3.2, AspectJ-1.6.11 Please let me know what am i doing wrong, as it is not giving any error, just the advice is not executing.
Below is the code sample
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("******");
}
}
Action Class 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;
}
}
Below is my spring configuration ApplicationContext.xml
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.example.action, com.igate.mldb" />
JSF Configuration 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>
Regards, Swapneel Killekar
Solution
I do not know much about Spring, but I know AspectJ syntax and can tell you that your @Around
advice's pointcut is missing a return type in the method signature. It should rather be like this:
@Around("* com.example.action.FirstAction.checkLogin()")
Or, more specific, because checkLogin
returns a String
:
@Around("String com.example.action.FirstAction.checkLogin()")
Consequently, your around advice cannot return void
, it needs to return Object
or String
too:
@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;
}
What I do not know is if your Spring config is correct, I can only help with the AspectJ part, sorry.
Answered By - kriegaex