简体   繁体   English

Spring Boot JPA不起作用

[英]Spring Boot JPA not working

I have a controller class which is below. 我有一个下面的控制器类。 I have a TagRepository interface which extends JPA repository which I am using to save TagReader instance to my DB and it works fine when I use it in my controller class. 我有一个TagRepository接口,该接口扩展了JPA存储库,该接口用于将TagReader实例保存到数据库中,并且在控制器类中使用它时可以正常工作。 However, when I try to use tagRepository in another class and try to save my TagReader object from there it throws a null pointer exception. 但是,当我尝试在另一个类中使用tagRepository并尝试从那里保存我的TagReader对象时,它将引发空指针异常。

The following logic works fine. 以下逻辑工作正常。

@RestController
public class Controller {

@Autowired
TagRepository tagRepository;

@Autowired
Rfid6204Connection rfid6204Connection;

@RequestMapping(value = "/test")
public void testRepoController(){

    String tagid = "0x3504ACE6E0040E5147D516A6";
    String serial ="00333478";
    String departure ="2017-12-22T12:16:58.857";
    String type = "ISOC";


    TagReader tagReader = new TagReader(tagid,serial,departure,type,"5");


    tagRepository.save(tagReader);
  }
}

The following logic throws a null pointer exception. 以下逻辑引发空指针异常。

@component
public class Rfid6204Connection{

    @Autowired
    static TagRepository tagRepository;

    public static void test(TagReader tag){
        tagRepository.save(tag);
    }

}

Can someone please tell me what the issue is? 有人可以告诉我问题是什么吗?

I think you are using Rfid6204Connection.test as a static method. 我认为您正在使用Rfid6204Connection.test作为静态方法。 Spring doesn't work with Static methods. Spring不适用于Static方法。 It works with Objects instantiated by the Spring Container. 它与由Spring容器实例化的对象一起使用。 So change your Rfid6204Connection as below; 因此,如下更改Rfid6204Connection

@Component
public class Rfid6204Connection{

    @Autowired
    private TagRepository tagRepository;

    public void test(TagReader tag){
        tagRepository.save(tag);
    }

}

And use it wherever you want as below; 并在以下任何位置使用它;

@Autowired 
Rfid6204Connection rfid6204Connection;

// Within a method or constructor
rfid6204Connection.test(tag);

You made the Autowired field static and when the class loader loads the static values, the Spring context is not yet loaded and your object is not correctly initialized; 您将Autowired字段设为 静态,并且当类加载器加载静态值时,Spring上下文尚未加载,并且您的对象未正确初始化。 remove the static keyword: 删除static关键字:

@Autowired
private TagRepository tagRepository;

you couldn't autowired static variables directly 你不能直接自动连接静态变量

then, you have some options. 然后,您有一些选择。 first, autowired instance of TagRepository and after dependency injection set a instance to static variable 首先,自动连接TagRepository实例,并在注入依赖项后将实例设置为静态变量

@Component
public class Rfid6204Connection {
private static TagRepository sTagRepository;

@Autowired
private TagRepository tagRepository;

@PostConstruct
public void init() {
    Rfid6204Connection.sTagRepository = tagRepository;
}
}

second prepare setter method of TagRepository and put a autowired 第二种准备TagRepository的setter方法并自动接线

public class Rfid6204Connection {

    private static TagRepository tagRepository;

    @Autowired
    public void setTagRepository(TagRepository tagRepository) {
        Rfid6204Connection.tagRepository = tagRepository;
    }

}

but originally ... you shoudn't autowire to static variables. 但是最初……您不应该自动连接到静态变量。

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

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