我创建了一个带有数据的mongodb,但是在检索特定集合中的所有文档时,我的程序会给出一个空指针异常。
这是我的代码,userrepository.java
package com.weatherdata.api.dbconnector;
import com.weatherdata.api.users.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserRepository extends MongoRepository<User,String> {
}
用户种子.java
package com.weatherdata.api.users;
import com.weatherdata.api.dbconnector.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class UserSeeder {
@Autowired
UserRepository userRepository;
public List<User> getAllUsers(){
List <User> allUsers = this.userRepository.findAll();
return allUsers;
}
}
用户.java
package com.weatherdata.api.users;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.HashSet;
import java.util.Set;
@Document(collection = "users")
public class User {
@Id
private String id;
private String username;
private String email;
private String mobile;
private String method;
private String password;
@DBRef
private Set<Role> roles = new HashSet<>();
public User() {
}
public User(String username, String email,String mobile,String method, String password) {
this.username = username;
this.email = email;
this.mobile = mobile;
this.method = method;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
错误是,
java.lang.nullpointerexception:无法调用“com.weatherdata.api.dbconnector.userrepository.findall()”,因为com.weatherdata.api.users.userseeder.getallusers(userseeder)处的“this.userrepository”为null。java:13)~[classes/:na]位于com.weatherdata.api.filter.alert.isexceeded(警报。java:20)~[classes/:na]在com.weatherdata.api.controller.sensorcontroller.insert(传感器控制器。java:56)~[classes/:na]位于java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:na]位于java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.invoke)。java:64)~[na:na]在java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)~[na:na]位于java.base/java.lang.reflect.method.invoke(method。java:564)~[na:na]位于org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod。java:197)~[spring-web-5.3.2。jar:5.3.2]在org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod。java:141)~[spring-web-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.mvc.method.annotation.servletinvaccablehandlermethod.invokeandhandle(servletinvaccablehandlermethod)。java:106)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter。java:894)~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter。java:808)~[spring-webmvc-5.3.2。jar:5.3.2] 位于org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter)。java:87)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet。java:1061)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet。java:961)~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet。java:1006)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet。java:909) ~[spring-webmvc-5.3.2。jar:5.3.2]
但restapi中的thig函数运行良好。
这是我在alert.java中使用usersedder的方法
package com.weatherdata.api.filter;
import com.weatherdata.api.users.User;
import com.weatherdata.api.users.UserSeeder;
import java.util.List;
public abstract class Alert implements AlertType {
private double thresholdValue;
private List<User> userList;
@Override
public double getThreshold() {
return this.thresholdValue;
}
@Override
public boolean isExceeded(double dataValue) {
if(thresholdValue < dataValue){
userList = new UserSeeder().getAllUsers();
System.out.println(userList.stream().count());
return true;
}
else{
return false;
}
}
public double getThresholdValue() {
return thresholdValue;
}
public void setThresholdValue(double thresholdValue) {
this.thresholdValue = thresholdValue;
}
}
2条答案
按热度按时间dgsult0t1#
必须在具有业务逻辑的类上使用steriotype annotation@service,在应用程序或实用程序类的所有层的通用类上使用@component,因为在主类上注解@springbootapplication时,它具有底层的@componentscan(basepackages=“package name where main class lies”)会自动将其删除创建basepackage下的类的示例。
f2uvfpb92#
你应该给班级做注解
UserSeeder
与@Service
或者@Component
为了让spring创建并处理这个类的一个示例作为spring管理的bean,这样依赖注入就可以正常工作,假设spring扫描包com.weatherdata.api.users
.另一种选择是定义
@Bean
在一个@Configuration
班级。您可以在许多文章和相关的在线文档(如链接1或链接2等)中找到关于spring中依赖注入的更多信息。
更新
当您手动示例化对象时,依赖注入不起作用
new UserSeeder()
解决问题的一个方法是Alert
子类spring也可以管理,并使用@Autowired
到类型的示例属性UserSeeder
从而消除new
用法。您可以使用(根据需要更改)