npe

z0qdvdin  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(344)

这个问题在这里已经有答案了

为什么我的spring@autowired字段为空(22个答案)
什么是nullpointerexception,如何修复它(12个答案)
两个月前关门了。
我在我的springboot项目中有下面的代码,该项目在前面提到的特定行中抛出npe。

  1. Exception in thread "main" java.lang.NullPointerException
  2. at com.temp.controller.Controller.triggerJob(Controller.java:15)
  3. at com.temp.Application.main(Application.java:19)

应用程序.java

  1. @Configuration
  2. @SpringBootApplication
  3. @ImportResource({"classpath:applicationContext.xml"})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. Controller controller = new Controller();
  8. controller.triggerJob();
  9. }
  10. }

控制器.java

  1. @Controller
  2. public class Controller {
  3. @Autowired
  4. private Service Service;
  5. public void triggerJob() {
  6. Service.selectRecords();
  7. }
  8. }

service.selectrecords();是npe被抛出的地方
服务.java

  1. public interface Service {
  2. List<VO> selectRecords();
  3. }

serviceimpl.java文件

  1. @Service
  2. public class ServiceImpl implements Service {
  3. @Autowired
  4. private Dao dao;
  5. @Override
  6. public List<VO> selectRecords() {
  7. return dao.selectRecords();
  8. }
  9. }

应用程序上下文.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8. http://www.springframework.org/schema/jdbc
  9. http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  10. http://www.springframework.org/schema/util
  11. http://www.springframework.org/schema/util/spring-util-3.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  14. <util:properties id="configProperties"
  15. location="file:${app.config.home}/config/config-${spring.profiles.active}.properties" />
  16. <context:property-placeholder
  17. location="file:${app.config.home}/config/config-${spring.profiles.active}.properties" />
  18. <bean id="crypt" class="com.temp.util.MyUtil">
  19. <property name="userId" value="${username}"></property>
  20. <property name="password" value="${password}"></property>
  21. <property name="key" value="123456789012345678901234"></property>
  22. </bean>
  23. <bean id="datasource"
  24. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  25. <property name="driverClassName" value="${driver}" />
  26. <property name="url" value="${connection}" />
  27. <property name="username" value="#{crypt.userId}" />
  28. <property name="password" value="#{crypt.password}" />
  29. </bean>
  30. <bean id="namedJdbcTemplate"
  31. class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
  32. <constructor-arg ref="datasource" />
  33. </bean>
  34. </beans>

我有一个类似的项目这样,并比较了两者的配置,他们是相同的,除了2件事。
springbootstarter版本对于这个项目是2.4.2,对于另一个项目是1.5.3。
这个项目的java版本是11,另一个项目是1.8。
不知道我哪里出错了。
我错过什么了吗?请帮忙。

nhjlsmyf

nhjlsmyf1#

您正在创建新示例 Controller controller = new Controller(); 而此示例不在spring上下文中。因此,服务的注入(自动连线)示例为空。 @Autowired 仅当示例存在于spring上下文中时才起作用。
最好的方法是保持控制器的可测试性,即通过构造函数注入:

  1. @Configuration
  2. @SpringBootApplication
  3. @ImportResource({"classpath:applicationContext.xml"})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. Controller controller = new Controller(new ServiceImpl(new DaoImpl()));
  8. controller.triggerJob();
  9. }
  10. }

通过构造函数注入示例:

  1. @Controller
  2. public class Controller {
  3. private final Service Service;
  4. public Controller(final Service Service) {
  5. this.service = service;
  6. }
  7. public void triggerJob() {
  8. Service.selectRecords();
  9. }
  10. }

并通过构造函数注入dao依赖关系:

  1. @Service
  2. public class ServiceImpl implements Service {
  3. private final Dao dao;
  4. public ServiceImpl(final Dao dao) {
  5. this.dao = dao;
  6. }
  7. @Override
  8. public List<VO> selectRecords() {
  9. return dao.selectRecords();
  10. }
  11. }

对于4.3以上的Spring版本, @Autowired 可以从构造函数的顶部省略,spring自动扫描注入并通过构造函数注入依赖项。对于4.3以下的spring版本,添加 @Autowired 在施工人员之上,即:

  1. @Controller
  2. public class Controller {
  3. private final Service Service;
  4. @Autowired
  5. public Controller(final Service Service) {
  6. this.service = service;
  7. }
  8. public void triggerJob() {
  9. Service.selectRecords();
  10. }
  11. }
展开查看全部

相关问题