简体   繁体   English

使用 mongoDB 的 Spring Boot 返回 NullPointerException

[英]Spring boot with mongoDB returns NullPointerException

Recently, I was trying to play with MongoDB and Spring Boot.最近,我正在尝试使用 MongoDB 和 Spring Boot。

I've seen multiple tutorials on how to do it, but I faced a problem when I tried to insert an object to the database.我看过多个关于如何执行此操作的教程,但是当我尝试将对象插入数据库时​​遇到了问题。 The issue is that, every tutorial or answer provided in SO implied that I will call the Repository that extends the MongoRepo through a REST Function (and as a Result the @Autowire annotation will instantiate the class).问题是,SO 中提供的每个教程或答案都暗示我将调用通过 REST 函数扩展 MongoRepo 的存储库(结果,@Autowire 注释将实例化该类)。 However, this is not the case for my program, as I will need to call the store class through a class located in a different package that is not being called via a REST API.但是,这不是我的程序的情况,因为我需要通过位于不同包中的类调用存储类,该类不是通过 REST API 调用的。 The first issue was with the scan of the different packages.I managed to solve it by using the baseScanPackages and the @EnableMongoRepository annotation.第一个问题是扫描不同的包。我设法通过使用 baseScanPackages 和 @EnableMongoRepository 注释来解决它。 I also created the MongoConfig class as the latter needed the MongoClient implementation.我还创建了 MongoConfig 类,因为后者需要 MongoClient 实现。

However, every time I call the class that stores my object, I get a NullPointerException .但是,每次调用存储对象的类时,都会得到NullPointerException I know that the problem is that ,since I call the class via a new Object and not Autowire it, the interface will not be instantiated.我知道问题在于,由于我通过新对象调用该类而不是自动装配它,因此不会实例化该接口。

My question is: Is there a way I can instantiate the interface that extends the MongoRepository without having to Autowire it?我的问题是:有没有一种方法可以实例化扩展 MongoRepository 的接口而不必自动装配它?

Below is the code:下面是代码:

Interface界面

@Repository
public interface TemplateRepository extends MongoRepository<Template,String> {
}

Store class店铺类

@Component
public class StoreTemplates {

    \\The issue is here. This class is never instantiated.
    private TemplateRepository templateRepository;
    @Autowired
    public StoreTemplates(TemplateRepository templateRepository) {
        this.templateRepository=templateRepository;
    }

    public void store(Template template){

        if(templateRepository!=null)
            templateRepository.save(template);
        else
            System.out.println("not initialised");
    }
}

Constructor of the class that calls the save method调用 save 方法的类的构造函数

StoreTemplates storeTemplates;
    /**
     * Constructor.
     * 
     * @param connection
     */
    public TemplateGeneral(Connection connection) {
        super(connection);
        storeTemplates = new StoreTemplates(); <- This will not trigger the @Autowired class
    }

Thank you!谢谢!

Last piece of code is incorrect where Instantiation is done via new keyword.最后一段代码不正确,其中实例化是通过 new 关键字完成的。 Until or unless you won't allow spring to do the injection which is the main purpose of using spring.直到或除非您不允许弹簧进行注射,这是使用弹簧的主要目的。 I don't see any way to get it directly.我没有看到任何直接获得它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM