java - Spring AOP :Cannot resolve method to a unique method

标签 java spring spring-mvc spring-aop

下面是方面java代码:

public class ParamAspect {
    public void beforeMethodExecution(String name){
         System.out.println("Name: " + name);
    }
    public void beforeMethodExecution(Employee employee){
         System.out.println("Employee Name: " + employee.getName());
    }
}

下面是 context.xml 文件:

<aop:config>
    <aop:pointcut id="empName" expression="execution(void com.model.Employee.setName(String)) and args(name1)"/>
    <aop:pointcut id="empl" expression="execution(void com.model.Employee.setEmployee(com.model.Employee)) and args(empl)"/>
    <aop:aspect ref="aspect">
        <aop:before pointcut-ref="empName" method="beforeMethodExecution" arg-names="name1"/>
        <aop:before pointcut-ref="empl" method="beforeMethodExecution" arg-names="empl"/>
    </aop:aspect>
</aop:config>

执行此操作时遇到的错误是:

Caused by: java.lang.IllegalArgumentException: Cannot resolve method 'beforeMethodExecution' to a unique method. Attempted to resolve to overloaded method with the least number of parameters, but there were 2 candidates.
at org.springframework.beans.BeanUtils.findMethodWithMinimalParameters(BeanUtils.java:290)
at org.springframework.beans.BeanUtils.findMethodWithMinimalParameters(BeanUtils.java:231)
at org.springframework.beans.BeanUtils.resolveSignature(BeanUtils.java:332)
at org.springframework.aop.config.MethodLocatingFactoryBean.setBeanFactory(MethodLocatingFactoryBean.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1475)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1443)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 38 more

但是,如果我更改方面的方法名称

`public void beforeMethodExecution(Employee employee){`

`public void beforeMethodExec(Employee employee){`

并在 context.xml 中进行相同的更改

<aop:before pointcut-ref="empl" method="beforeMethodExecution" arg-names="empl"/>

<aop:before pointcut-ref="empl" method="beforeMethodExec" arg-names="empl"/>

然后代码就可以正常工作了。

请告诉我为什么同名方法会抛出异常。

最佳答案

尝试使用:

<aop:config>
    <aop:pointcut id="empName" expression="execution(void com.model.Employee.setName(String)) and args(name1)"/>
    <aop:pointcut id="empl" expression="execution(void com.model.Employee.setEmployee(com.model.Employee)) and args(empl)"/>
    <aop:aspect ref="aspect">
        <aop:before pointcut-ref="empName" method="beforeMethodExecution(String)"/>
        <aop:before pointcut-ref="empl" method="beforeMethodExecution(com.model.Employee)"/>
    </aop:aspect>
</aop:config>

关于java - Spring AOP :Cannot resolve method to a unique method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065975/

相关文章:

json - RESTful json 响应在 tomcat 上失败但在 GlassFish 中有效

java - Spring MVC 应用程序在 "incorrect"URL 上运行

java - 当我们调用函数ZipEntry.getSize时,库是否首先在内存中解压缩文件?

java - Spring Batch JdbcBatchItemWriter

java - 是什么让我的代码使用这么多内存?

Spring MVC @RestController 和重定向

java - 如何将 Spring 服务委托(delegate)给不同的数据库?

java - 用于浏览器和 Web 服务的 Spring

java - 使用java应用程序发送电子邮件

java - 在独立应用程序中使用 Spring