java.lang.IllegalArgumentException:找到多个候选参数类型:[java.lang.String]和[java.lang.Long]

时间:2016-06-10 15:13:18

标签: java spring spring-integration

我在我的GetAccountsDAO中有了intitally findByNameAndId(String name)。后来,需求发生了变化,我不得不在其中添加id(Long)。

添加实现后,我想测试它。它开始失败。

/**
 * The interface that is used to get account
 */
public interface GetAccountsDAO {
    @SuppressWarnings("unchecked")
    public List<Account> findByNameAndId(String name, Long id);
}

错误日志是:

java.lang.IllegalArgumentException: Found more than one parameter type candidate: [java.lang.String] and [java.lang.Long]
at org.springframework.util.Assert.isNull(Assert.java:89)
at org.springframework.integration.util.MessagingMethodInvokerHelper$HandlerMethod.setExclusiveTargetParameterType(MessagingMethodInvokerHelper.java:624)
... ...

...ramework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1838ccb8] to prepare test instance [com.project.integration.account.GetAccountMockTest@6c2ed0cd]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
... ...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.handler.MessageHandlerChain#0': Cannot create inner bean 'org.springframework.integration.config.ServiceActivatorFactoryBean#6ff29830' of type [org.springframework.integration.config.ServiceActivatorFactoryBean] while setting bean property 'handlers' with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.ServiceActivatorFactoryBean#6ff29830': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Target object of type [class com.project.dao.account.GetAccountsDAO$$EnhancerByMockitoWithCGLIB$$83f498fd] has no eligible methods for handling Messages.
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282)
... ...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.ServiceActivatorFactoryBean#6ff29830': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Target object of type [class com.project.dao.account.GetAccountsDAO$$EnhancerByMockitoWithCGLIB$$83f498fd] has no eligible methods for handling Messages.
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:109)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:275)
    ... 49 more
... ...

Caused by: java.lang.IllegalArgumentException: Target object of type [class com.project.dao.account.GetAccountsDAO$$EnhancerByMockitoWithCGLIB$$83f498fd] has no eligible methods for handling Messages.
    at org.springframework.util.Assert.notEmpty(Assert.java:294)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:352)

1 个答案:

答案 0 :(得分:2)

您必须阅读更多documentation

  

在第二种情况下(或者当参数foo的参数是Map时的第一种情况),框架无法确定哪个参数应该是payload;映射将失败。通常可以使用payload-expression@Payload注释和/或@Headers注释解决此问题。

由于您的String name, Long id都是原始类型,并且无法明确确定哪个属于payload,因此框架会抛出Found more than one parameter type candidate异常。