Spring Boot @Autowired对象-Nullpointer异常

我正在开发一个Spring Boot应用程序来发送短信通知。这是我上课的目的。

package org.otp.services;

import org.otp.Configurations;
import com.mashape.unirest.http.HttpResponse;
import org.springframework.stereotype.Component;

import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Component
public class SmsService
{
    private static final Logger LOG = LoggerFactory.getLogger(SmsService.class);

    public String send(String mobile,String msg)
    {
        //Code 
    }
}

这是使用上述类发送通知的类。

package org.otp.controllers;

import org.otp.Constants;
import org.otp.services.EmailService;
import org.otp.services.SmsService;
import org.otp.dto.MessageRequest;
import org.otp.dto.MessageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

@Component
public class MessageController {

    private static final Logger LOG = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    SmsService smsService;

    public void sendMessageToAlert(@RequestBody MessageRequest messageRequest)
    {
        String smsStatus = "FAIL";
        MessageResponse messageResponse = new MessageResponse();

         //1. Nullpointer
        smsStatus = smsService.send(messageRequest.getMobileNo(),messageRequest.getMessage());

    }
}

主类

package org.otp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class OtpServiceApplication implements ApplicationRunner
{
    public static void main(String[] args) {
        SpringApplication.run(OtpServiceApplication.class,args);
    }
}

问题是,我在(1)中得到一个 nullpointer异常,说明我的 SmsService 对象为null。而且我的主类在软件包org.otp中,所以这里的两个类都在子软件包下,因此不需要组件扫描。

因此,我很困惑该怎么办。我在这里尝试了许多答案,例如在主类中添加了@Component批注和@ComponentScan,但没有任何效果。有人可以在这里指出我的错误。

谢谢。

jackychen101211 回答:Spring Boot @Autowired对象-Nullpointer异常

如果您的@Autowired注释不起作用并抛出NPE,则意味着spring无法在应用程序上下文中创建组件类的实例。尝试:

  • 验证类是否在类路径中以进行扫描,并检查以确保所有自动连线的类都具有注释@Component,以使它们能够在类路径扫描期间被拾取。
  • 检查spring boot启动日志以验证是否有任何错误 在bean创建过程中。
  • 检查以确保正确连接了服务层中使用的所有相关类,并确保使用@Component注释注入的类。

要获得更多帮助,请与您的项目结构一起共享主应用程序类。


由于您使用的是springboot,因此,如果要构建标准的springboot Web应用程序,则最好使用sprinboot构造型注释而不是@Component注释。

  • @Service:用于服务层。
  • @Controller:用于控制器层。此外,DispatcherServlet将在使用@RequestMapping注释但没有使用@Controller注释的类上寻找@Component
,

在Springboot应用程序的主类中添加以下注释

@SpringBootApplication
@ComponentScan(
    basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
  public static void main(String[] args) {
    SpringApplication.run(YourSpringMainClass.class,args);
  }
}
,

在使用批注的同时,我们应配置@ ComponentScan 批注,以告知Spring软件包以扫描带批注的组件。在使用Spring Boot的情况下,应在邮件类(哪个类要首先加载)中使用,因此应在Springboot应用程序的主类中使用此批注。像下面一样

@SpringBootApplication
@ComponentScan(
    basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
  public static void main(String[] args) {
    SpringApplication.run(YourSpringMainClass.class,args);
  }
}
本文链接:https://www.f2er.com/3126547.html

大家都在问