java - Spring AOP : Trying to use for logging but getting IllegalArgumentException

标签 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);   
}

感谢对此的任何帮助并表示感谢。

最佳答案

您的代码中有 2 个问题

  1. 您的切入点与实际包不匹配
  2. 您的建议方法只有一个参数,而预期有 2 个参数。

首先修复包中的切入点表达式。 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,..)

关于java - Spring AOP : Trying to use for logging but getting IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531996/

相关文章:

java - 获取 javax.mail.MessagingException 和 java.net.SocketException

Java - 更改原始类型变量的值。可能的?

java - 在 spring java 程序中获取消息 "The type org.aopalliance.aop.Advice cannot be resolved."

c# - 如何使用属性名称和前缀从属性方面调用特定方法?

java - 是否可以编写测试事务代码的事务单元测试?

java - Java::server 中的客户端服务器程序不从文件中逐行读取文本

java - 如何使用 ScrollView ?

java - 为什么在 Hibernate 抛出 ObjectNotFoundException 时 Spring 不抛出 DataAccessException?

java - 我们如何在 CrudRepository 中实现自定义查找方法以仅从数据库中获取特定列?

java - log4j2.xml 已加载但未应用 [JVM 参数]