Spring的AspectJ建议没有执行

时间:2014-08-08 03:56:01

标签: spring jsf jsf-2 aspectj jsf-2.2

我尝试在执行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;
    }
}

以下是我的弹簧配置 的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 个答案:

答案 0 :(得分:1)

我对Spring不太了解,但我知道AspectJ语法并且可以告诉您,@Around建议的切入点缺少方法签名中的返回类型。它应该是这样的:

@Around("* com.example.action.FirstAction.checkLogin()")

或者更具体一点,因为checkLogin会返回String

@Around("String com.example.action.FirstAction.checkLogin()")

因此,您的周围建议无法返回void,它也需要返回ObjectString

@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部分,抱歉。