spring 数据 mongo 存储库为空 [英] spring data mongo repository is null

查看:33
本文介绍了spring 数据 mongo 存储库为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个独立的 java 程序,它调用 spring 数据存储库来管理实体.我正在使用 mongo db 进行持久化.我正在关注 stackoverflow 帖子和来自 github 的一些项目,但是当我运行我的程序时它失败了,因为存储库为空.我不是春天的专家,所以如果有人能告诉我发布的程序的问题会很有帮助.

I am writing a standalone java program that call spring data repository for manging enities. I am using mongo db for persistence. I am following stackoverflow posts and some projects from github but when I run my program it fails since the repository is null. I am not expert in spring so it would be helpful if someone could show me the issue with the posted program.

应用程序上下文.xml

Application-context.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:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/data/mongo 
    http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">


<mongo:mongo id="mongo" host="monopolyvm3" port="27017" />
<mongo:db-factory dbname="test" mongo-ref="mongo" />
<mongo:db-factory id="mongoDbFactory" dbname="cloud"
    mongo-ref="mongo" />


<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

<bean id="defaultMongoTypeMapper"
    class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
</bean>

<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mappingContext" />
    <property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    <property name="writeConcern" value="SAFE" />
</bean>
<context:component-scan base-package="com.xxxx"></context:component-scan>

<mongo:repositories base-package="com.xxxx.yyyy" />

public interface SlpRepository extends MongoRepository<Slp, Long> {

}

主程序

public class App {

    @Autowired
    static
    SlpRepository slpRepository;
    public static void main(String[] args) {
        ApplicationContext ctx = new GenericXmlApplicationContext("application-context.xml");
        Slp slp = new Slp();
        slp.setClientCount(100000L);
        slp.setPolicyName("testing");
        slp.setSlpName("slp_testing");
        slpRepository.save(slp);

    }


}

Object to store
------------------

@Document(collection="slps")
public class Slp implements Serializable {


    private Long slpId;
    private String slpName;
    private String policyName;
    private Long clientCount;
}
.....all getters and setters

JFYI..我尝试使用 mongotemplate 保存对象,效果很好.

JFYI..I tried to save object using mongotemplate and it works well.

推荐答案

此示例中的问题是您在 App 类中创建了上下文,但从未从应用程序上下文中获得存储库的句柄.您不能自动连接到保存应用程序上下文的同一个类.您可以尝试从上下文中获取存储库的实例(即 ctx.getBean(SlpRepository.class)).

The problem in this example is that you create the context in your App class but never get a handle to the repository from the application context. You cannot auto wire into the same class that holds the application context. What you can try is the get the instance of the repository from the context (i.e ctx.getBean(SlpRepository.class)).

这篇关于spring 数据 mongo 存储库为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆