Java Spring Boot@Autowired对象-空指针异常
我正在开发一个spring启动应用程序来发送sms通知。这是我的课程Java Spring Boot@Autowired对象-空指针异常,java,spring,spring-boot,nullpointerexception,autowired,Java,Spring,Spring Boot,Nullpointerexception,Autowired,我正在开发一个spring启动应用程序来发送sms通知。这是我的课程 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;
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)中得到了一个空指针异常,表明我的SmsService对象为空。我的主类在packageorg.otp
中,因此这里的两个类属于子包,因此不需要组件扫描
因此,我不知道如何解决这个问题。我在这里尝试了很多答案,比如在主类中添加@Component
注释和@ComponentScan
,但没有任何效果。有人能指出我的错误吗
提前感谢。如果您的
@Autowired
注释不起作用并抛出NPE,则表示spring无法在应用程序上下文中创建组件类的实例。尝试:
- 验证类是否位于用于扫描的类路径中,并检查以确保所有自动连接的类都具有注释
,以便在类路径扫描期间能够拾取它们@组件
- 检查spring启动日志以验证是否存在任何错误 在bean创建期间
- 检查以确保服务层中使用的所有相关类都已正确自动连接,并且注入的类已用
注释@Component
由于您使用的是springboot,因此如果您正在构建标准springboot web应用程序,最好使用sprinboot原型注释,而不是
@Component
注释
:用于服务层@Service
:用于控制器层。此外,DispatcherServlet将在使用@Controller
注释但不使用@Controller
注释的类上查找@Component
@RequestMapping
@SpringBootApplication
@ComponentScan(
basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
public static void main(String[] args) {
SpringApplication.run(YourSpringMainClass.class, args);
}
}
在使用注释时,我们应该配置@ComponentScan注释,告诉Spring软件包扫描带注释的组件。在您使用Springboot的情况下,应该在mail类(哪个类要首先加载)中使用此注释,所以您应该在Springboot应用程序的主类中使用此注释。如下
@SpringBootApplication
@ComponentScan(
basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
public static void main(String[] args) {
SpringApplication.run(YourSpringMainClass.class, args);
}
}
messageRequest
null不是吗?显示调用sendmagestoalert
的类。请将主类发布为well@yeahseol实际上,我省略了这个问题的无关代码。Mark I发布了一个主类和一个编辑器。您可以展示您的SmsService
实现吗?@Service
现在很少使用,因为它在实践中与@Component
没有什么不同。这与此问题无关(尤其与引导无关)。Spring Boot会自动添加此功能,如果您像询问者那样遵循指导原则,则不需要@ComponentScan
。Spring Boot会自动添加此功能,如果您像询问者那样遵循指导原则,您不需要@ComponentScan
。