У меня есть небольшое веб-приложение в разработке с использованием Maven, Spring MVC и Spring Data Mongo. Я получаю java.lang.NoSuchMethodError, когда один из моих контроллеров пытается получить доступ к методу, определенному в пользовательском репозитории. Тот же метод отлично работает при проверке с помощью теста JUnit 4, расширяющего AbstractJUnit4SpringContextTests и использующего почти идентичный файл конфигурации XML.
Стандартный репозиторий:
public interface IndividualRepository extends MongoRepository<Individual, String>, IndividualRepositoryCustom {
...
}
Пользовательский интерфейс:
public interface IndividualRepositoryCustom {
Individual findByIdentifier(String identifierType, String identifierValue);
}
Пользовательская реализация:
public class IndividualRepositoryImpl implements IndividualRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public Individual findByIdentifier(String identifierType, String identifierValue) {
String locator = String.format("identifiers.%s", identifierType);
return mongoTemplate.findOne(query(where(locator).is(identifierValue)), Individual.class);
}
}
доступ к данным-config.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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:repositories base-package="com.myco.dataaccess"/>
<mongo:mongo host="mongo.myco.com" port="27017"/>
<mongo:db-factory dbname="test" mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg value="test"/>
</bean>
</beans>
В моем тесте JUnit у меня есть (отрывок):
@Autowired
private IndividualRepository individualRepo;
...
List<Individual> foundList = individualRepo.findAll();
assertNotNull(foundList);
assertTrue(foundList.size() > 0);
Individual found = individualRepo.findByIdentifier("someid", "123456");
assertNotNull(found);
assertEquals("Bob", found.getFirstName());
Тест проходит нормально, вызывая как findAll() (стандартный метод репозитория), так и findByIdentifier() (пользовательский метод). Последний завершается с ошибкой NoSuchMethodError при вызове контроллера, работающего в веб-приложении в Jetty, в то время как тот же контроллер может вызывать findAll() без проблем.