Java @Autowired不会注入Mongodb存储库或服务-nullPointerException-
我正在使用连接到服务中的mongodb存储库,我想将其连接到另一个组件中,以便使用findAllJava @Autowired不会注入Mongodb存储库或服务-nullPointerException-,java,mongodb,spring-boot,nullpointerexception,autowired,Java,Mongodb,Spring Boot,Nullpointerexception,Autowired,我正在使用连接到服务中的mongodb存储库,我想将其连接到另一个组件中,以便使用findAll @Service @Transactional public class SettingService implements SettingInterface { public SettingService() { } private SettingRepository settingRepository; @Autowired public SettingService(
@Service
@Transactional
public class SettingService implements SettingInterface {
public SettingService() {
}
private SettingRepository settingRepository;
@Autowired
public SettingService(SettingRepository settingRepository) {
this.settingRepository = settingRepository;
}
@Override
public List<Setting> findAll() {
return settingRepository.findAll();
}
}
@服务
@交易的
公共类SettingService实现SettingInterface{
公共设置服务(){
}
私有设置存储库设置存储库;
@自动连线
公共设置服务(设置存储库设置存储库){
this.settingRepository=settingRepository;
}
@凌驾
公共列表findAll(){
返回setingrepository.findAll();
}
}
存储库
public interface SettingRepository extends MongoRepository<Setting, String> {
}
public interface SettingRepository扩展了MongoRepository{
}
类,我想将数据注入其中
@Component
public class Config {
private SettingService service;
public Config() {
}
@Autowired
public Config(SettingService service) {
this.service = service;
}
// The doSomething method is being called from a constructor
public Order(List<Item> listOfItems) {
// Initialize ArrayLists
order = new ArrayList<>();
results = new ArrayList<>();
possibleResults = new ArrayList<>();
Accepted = new HashMap<>();
doSomething();
// Soft copy order
order.addAll(listOfItems);
}
@Transactional
public void doSomething() {
List<Setting> settingList = service.findAll();
// Manipulate Data
}
}
@组件
公共类配置{
私人设置服务;
公共配置(){
}
@自动连线
公共配置(设置服务){
服务=服务;
}
//正在从构造函数调用doSomething方法
公共秩序(项目列表){
//初始化数组列表
订单=新的ArrayList();
结果=新的ArrayList();
possibleResults=newArrayList();
已接受=新HashMap();
doSomething();
//软拷贝订单
order.addAll(列表项);
}
@交易的
公共无效剂量测定法(){
List settingList=service.findAll();
//操纵数据
}
}
我遇到的问题是,服务注入返回null,我在调试模式下运行应用程序,并在调用findAll的地方放置一个断点,它抛出nullPointerException,服务没有被注入
我还尝试将存储库直接自动连接到我的配置类中,而不是服务中,但结果与nullPointerException相同
PS:相同的配置和相同的服务注入似乎在我的一个控制器中工作得很好,服务连接成功,但它不适用于我的@组件。还尝试使用@Service、@Transactiona,但结果相同
- Spring boot版本2.2.4.0发布
- Mongodb:3.11.2
@Repository
public interface SettingRepository extends MongoRepository<Setting, String> {
}
@存储库
公共接口设置存储库扩展了MongoRepository{
}
我的组件类
// tried it with @Component as well
@Service
@Transactional
public class Config {
private SettingRepository SettingRepository;
@Autowired
public Config(SettingRepository SettingRepository) {
this.SettingRepository = SettingRepository;
}
public Config() {
}
public Config(List<Item> listOfItems) {
// Initialize ArrayLists
order = new ArrayList<>();
possibleConfigurations = new ArrayList<>();
acceptedConfigurations = new ArrayList<>();
mapOfAcceptedConfigurations = new HashMap<>();
doSomething();
// Soft copy order
order.addAll(listOfItems);
}
public void doSomething() {
List<Setting> settingList = SettingRepository.findAll();
Level securityLevel = settingList.get(0).getSecurityLevel();
Level fragileAndHeavyLevel =settingList.get(0).getLevelFragileItem();
}
//也尝试了@Component
@服务
@交易的
公共类配置{
私有设置存储库设置存储库;
@自动连线
公共配置(设置存储库设置存储库){
this.SettingRepository=SettingRepository;
}
公共配置(){
}
公共配置(列表项){
//初始化数组列表
订单=新的ArrayList();
possibleConfigurations=newArrayList();
acceptedConfigurations=new ArrayList();
mapOfAcceptedConfigurations=新HashMap();
doSomething();
//软拷贝订单
order.addAll(列表项);
}
公共无效剂量测定法(){
List settingList=SettingRepository.findAll();
级别securityLevel=settingList.get(0.getSecurityLevel();
Level fragileAndHeavyLevel=settingList.get(0.getLevelFragileItem();
}
这产生了同样的结果,我想知道如果有多个构造函数,是否会导致自动连接为空。我只自动连接一个注入repo的构造函数,我们可以在应用程序中使用@Component将bean标记为Spring的托管组件。Spring只在@Component和d中拾取并注册beanoesn一般不寻找@Service和@Repository 所以,若未指定组件扫描顺序,则无法在组件中自动连接@Service和@Repository
如果要自动连线并在存储库类上使用@Repository,请使用@Configuration而不是@Component。更新解决方案: 我找到了问题的根源,我从这个构造函数调用doSomething
public Config(List<Item> listOfItems) {
// Initialize ArrayLists
order = new ArrayList<>();
possibleConfigurations = new ArrayList<>();
acceptedConfigurations = new ArrayList<>();
mapOfAcceptedConfigurations = new HashMap<>();
doSomething();
// Soft copy order
order.addAll(listOfItems);
}
在调用
doSomething()
的地方显示代码。您可能在尝试自动连接mongoRepo时得到空指针,因为您没有对它进行注释,所以它不会是可注入bean。@123在中注释constructor@AbinashGhosh这只是告诉它自动连接它(也不需要,因为autowire在单个构造函数bean中是隐式的),它应该会失败,出现0 bean异常,因为SettingRepository
不可用annotated@123你的意思是在repository类中注释对吗?我没有理解。这是错误的,@Service
和@repository
只是专门的@Component
s,你完全可以在@组件中自动连接beanode>@Configuration
用于定义bean。@123如果未指定组件扫描顺序,则无法自动连线。您不必在Spring boot中指定。@123是否确定?可能是我在Spring boot中也遇到了相同的问题我在任何版本的Spring boot中都没有遇到过该问题,我拥有的任何文档也没有遇到过该问题看到状态您必须指定扫描顺序。如果您发布一个问题,其中包含一个显示该行为的项目的最小示例,那么我可以看一看。我假设您包含一个注释或执行一些覆盖spring引导默认值的配置。
@Autowired
public Config(SettingRepository settingRepository) {
this.settingRepository = settingRepository;
doSomething();
}