我正在开发一个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
中,所以这里的两个类属于子包,所以不需要组件扫描。
因此,我很困惑该怎么办才能解决这个问题。我在这里尝试了很多答案,比如在main类中添加@Component
注释和@ComponentScan
,但都不起作用。有人能在这里指出我的错误吗。
提前谢谢。
如果@Autowired
注释不起作用并抛出NPE,则意味着spring无法在应用程序上下文中创建组件类的实例。尝试:
- 验证类是否在要扫描的类路径中,并检查以确保所有自动连接的类都具有注释
@Component
,以使它们能够在类路径扫描期间被拾取 - 检查弹簧启动日志以验证是否存在任何错误在bean创建期间
- 检查以确保服务层中使用的所有相关类都是正确自动连接的,并且注入的类都用
@Component
进行了注释
如需进一步帮助,请与您的项目结构一起共享主应用程序类。
由于您使用的是springboot,如果您正在构建标准springboot web应用程序,则最好使用springboot构造型注释而不是@Component
注释。
@Service
:用于服务层@Controller
:用于控制器层。此外,DispatcherServlet将在使用@Controller
注释但不使用@Component
注释的类上查找@RequestMapping
在Springboot应用程序的主类中添加以下注释
@SpringBootApplication
@ComponentScan(
basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
public static void main(String[] args) {
SpringApplication.run(YourSpringMainClass.class, args);
}
}
在使用注释时,我们应该配置@ComponentScan注释,以告诉Spring包扫描带注释的组件。在您使用spring-boot的情况下,这应该在mail类(哪个类想要首先加载(中使用,所以您应该在spring-boots应用程序的主类中使用这个注释。像下面的
@SpringBootApplication
@ComponentScan(
basePackages = {"org.otp.*"}
)
public class YourSpringMainClass{
public static void main(String[] args) {
SpringApplication.run(YourSpringMainClass.class, args);
}
}