我有一个使用 Spring/Hibernate 的 Maven 项目,并且在 this.userService.save(user);
处不断收到 NullPointerException。 UserService 是 @Autowired,所以我不确定为什么这会抛出 NPE。
UserService 的代码示例:
@Component
public class BotService extends TelegramLongPollingBot {
@Autowired
private UserService userService;
@Autowired
private MessagesService messagesService;
@Override
public void onUpdateReceived(Update update) {
Message message = update.getMessage();
String text = message.getText();
User user = new User();
user.setTelegramId(message.getFrom().getId());
user.setFirstName(message.getFrom().getFirstName());
user.setLastName(message.getFrom().getLastName());
Messages messages = new Messages();
messages.setUser(user);
messages.setId(user.getId());
messages.setText(text);
this.userService.save(user); // <-- NPE
}
}
UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.model.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
User findById(int id);
}
UserService.java
import com.example.model.User;
public interface UserService {
public User findById(int id);
public void save(User user);
}
UserServiceImpl.java
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.model.User;
import com.example.repository.UserRepository;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserRepository userRepository;
@Override
@Transactional
public void save(User user) {
userRepository.save(user);
}
@Override
public User findById(int id) {
return userRepository.findById(id);
}
}
UPD。添加了详细说明
BotExampleApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.starter.EnableTelegramBots;
import com.example.service.BotService;
@SpringBootApplication
@EnableTelegramBots
@EnableTransactionManagement
public class BotExampleApplication {
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
try {
botsApi.registerBot(new BotService());
} catch (TelegramApiException e) {
e.printStackTrace();
}
SpringApplication.run(BotExampleApplication.class, args);
}
}
堆栈跟踪:
java.lang.NullPointerException: null
at com.example.service.BotService.onUpdateReceived(BotService.java:46) ~[classes/:na]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1380) ~[na:na]
at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) ~[telegrambots-meta-3.6.1.jar:na]
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309) ~[telegrambots-3.6.1.jar:na]
有什么建议吗?
最佳答案
您永远不应该创建 Spring 组件的新实例。在您的主类中,BotService
是使用new
创建的。这与 spring 创建的 BotService 实例不同。 @Autowired
仅适用于 Spring 组件,并且由于您使用的是不同的实例,因此依赖项永远不会注入(inject)其中。
要使其正常工作,请从 spring 上下文中获取 BotService
实例,然后使用它进行注册。
ConfigurableApplicationContext context = SpringApplication.run(...);
BotService service = context.getBean(BotService.class);
关于java - UserService NullPointerException Spring/Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783771/