我在堆栈溢出和 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 个问题
- 您的切入点与实际包不匹配
- 您的建议方法只有一个参数,而预期有 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/