Spring AOP:尝试用于日志记录但是获取IllegalArgumentException

时间:2014-03-20 11:47:02

标签: java spring aop spring-aop

我已经看到类似的问题发布在堆栈溢出和spring论坛上,但无法找到解决方案。当我尝试访问主页时 - 所以这是在我尝试点击登录之前 - 我得到以下异常:

java.lang.IllegalArgumentException: warning no match for this type name: enteredPassword [Xlint:invalidAbsoluteTypeName]

这是我的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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


<bean class="com.company.controller.UserValidation" id="userValidation"/>
<bean class="com.company.model.ActivityLogging" id="activityLogging"/>

<aop:config>
    <aop:aspect ref="activityLogging">
        <aop:pointcut id="validateUser" expression="execution(* com.company.controller.UserValidation.validateUser(java.lang.String,java.lang.String)) and args(username, enteredPassword)"/>
        <aop:before pointcut-ref="validateUser" method="logLoginAttempt" arg-names="username"></aop:before>
    </aop:aspect>
</aop:config>

</beans>

这是validateUser()方法:

public User validateUser(String username, String enteredPassword) throws NoResultException, PasswordIncorrectException{
    User user = dal.searchForUser(username);
    if(user.getPassword().equals(enteredPassword)){
        return user;
    }else{
        throw new PasswordIncorrectException();
    }
}

这是记录方法:

public void logLoginAttempt(String username){
    activityLogger.info("Login attempt by: " + username);   
}

感谢您的任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

您的代码中有2个问题

  1. 您的切入点与实际包裹
  2. 不符
  3. 您的建议方法只有一个参数,而预期为2。
  4. 首先修复切入点表达式中的包。 com.company.model.controller.UserValidation应为com.company.controller.UserValidation

    <aop:config>
        <aop:aspect ref="activityLogging">
            <aop:pointcut id="validateUser" expression="execution(* com.company.controller.UserValidation.validateUser(java.lang.String,java.lang.String)) and args(username, enteredPassword)"/>
            <aop:before pointcut-ref="validateUser" method="logLoginAttempt" arg-names="username"></aop:before>
        </aop:aspect>
    </aop:config>
    

    在这方面,您已指定args(username, enteredPassword),要求您的建议有2个参数。

    将这些添加到您的配置和建议中。

    <aop:before pointcut-ref="validateUser" method="logLoginAttempt" arg-names="username,enteredPassword">
    
    public void logLoginAttempt(String username, String enteredPassword){
        activityLogger.info("Login attempt by: " + username);   
    }
    

    或者从args子句args(username,..)中删除它。