gpt4 book ai didi

spring-data-mongodb - 自定义存储库界面出错

转载 作者:行者123 更新时间:2023-12-04 08:13:09 26 4
gpt4 key购买 nike

我正在尝试在多模块 Maven 3 项目中使用 Spring Data for MongoDB 设置我的第一个 Java 应用程序。以下是相关版本:

  • Java 7
  • mongodb-win32-x86_64-2.2.0
  • Spring Data 1.1.1.RELEASE
  • Spring 3.2.0.RELEASE

我收到以下运行时错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actorFacade': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.mi.casetools.praetor.persistence.springdata.repositories.ActorRepository es.mi.casetools.praetor.facade.impl.DefaultActorFacade.actorRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actorRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property insert found for type void
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:106)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:57)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 30 more

在谷歌搜索我发现有人有同样的问题,它似乎与自定义存储库有关。

这是我要存储为 mongo 文档的实体。

public class Actor {
public enum ActorStereotype {
SYSTEM,
PERSON
}
private String id;
private String code; // unique
private ActorStereotype stereotype;
private String title; // Short title for the actor
private String description;
private String projectId; // project this actor belongs to

// getters & setters

标准存储库界面。

public interface ActorRepository extends MongoRepository<Actor, String>, ActorRepositoryCustom {
}

自定义界面(我认为错误所在)。

@NoRepositoryBean
public interface ActorRepositoryCustom {
void updateSingleActor(Actor actor);
void insertActor(Actor actor);
}

自定义接口(interface)实现。

public class ActorRepositoryCustomImpl implements ActorRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;

@Override
public void updateSingleActor(Actor actor) {
if(actor.getId() != null)
throw new MissingIdException();

// TODO change to Spring Converter
DBObject dbo = (DBObject)mongoTemplate.getConverter().convertToMongoType(actor);

mongoTemplate.updateFirst(query(where("_id").is(actor.getId())),
Update.fromDBObject(dbo, new String[]{}),
Actor.class);
}

@Override
public void insertActor(Actor actor) {
if(actor.getId() != null)
throw new IdProvidedException();

mongoTemplate.save(actor);
}

}

最后,应用上下文。

    <context:annotation-config/>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/test.properties</value>
</list>
</property>
</bean>

<!-- mongodb configuration -->
<mongo:repositories base-package="es.mi.casetools.praetor.persistence.springdata.repositories"
mongo-template-ref="mongoTemplate" repository-impl-postfix="Impl">
<repository:exclude-filter type="annotation" expression="org.springframework.data.repository.NoRepositoryBean"/>
</mongo:repositories>

<mongo:mongo id="mongotest" host="${mongo.host}" port="${mongo.port}" write-concern="SAFE">
</mongo:mongo>

<mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongotest"/>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<bean id="actorFacade" class="es.mi.casetools.praetor.facade.impl.DefaultActorFacade">
</bean>

</beans>

我还有一个小的 Spring 测试未能加载上面的应用程序上下文,给出了我在顶部附近列出的异常。

我尝试添加以下内容,但出现相同的异常。

<bean id="actorRepositoryCustomImpl" class="es.mi.casetools.praetor.persistence.springdata.repositories.ActorRepositoryCustomImpl"></bean>

有人知道错误可能是什么吗?

最佳答案

Miguel 的评论解决了这个问题。我给实现类起了错误的名字。我已经看到了类似的问题,所以我会尝试澄清解决方案,希望它能帮助其他人。

我有以下接口(interface)定义

public interface ActorRepository extends MongoRepository<Actor, String>, ActorRepositoryCustom

自定义接口(interface)定义如下所示:

public interface ActorRepositoryCustom

所以我的错误是将 ActorRepositoryCustom 的实现命名为 ActorRepositoryCustomImpl,期望 Springdata 会选择实现,因为它的后缀是默认的 Impl。问题是,Springdata 默认查找 ActorRepositoryImpl,即使您正在实现的是 ActorRepositoryCustom。解决方案是使用可选属性 repository-impl-postfix 并将其设置为 CustomImpl

关于spring-data-mongodb - 自定义存储库界面出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com