【问题标题】:All my @Autowired beans are null我所有的@Autowired bean 都是空的
【发布时间】:2018-07-09 23:04:30
【问题描述】:

我无法弄清楚为什么我所有的 bean 在控制器中都是空的。我知道这是一个常见问题,但我没有使用 new 实例化对象。

控制器:

@Controller
@RequestMapping("/store")
public class MyController {

    @Autowired
    private MyService myService;

    @GetMapping("/getOptions")
    private String getOptions(HttpServletRequest request)
    {
        myService.doSomething(request);
        ....
    }
}

当我请求 http://localhost/store/getOptions 时,我在 myService 上收到 NullPointerException。

我的服务

@Service
public class MyService
{
    ....
}

网络配置

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    BeanNameUrlHandlerMapping beanNameUrlHandlerMapping()
    {
        return new BeanNameUrlHandlerMapping();
    }
}

主要

@SpringBootApplication
@Configuration
@ComponentScan(basePackages = { "com.mypackage.config", "com.mypackage.service", "com.mypackage.controller"})
public class MyApplication
{
    public static void main(String[] args)
    {
        if (AuthConfigFactory.getFactory() == null)
            AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());
        SpringApplication.run(MyApplication.class, args);
    }
}

当我启动应用程序时,我可以在日志中看到 myService bean 实际上正在自动装配到控制器中:

- Processing injected element of bean 'myController': AutowiredFieldElement for private com.mypackage.service.MyService com.mypackage.controller.MyController.myService
- Returning cached instance of singleton bean 'MyService'
- Autowiring by type from bean name 'myController' to bean named 'myService'

所以我不明白为什么当我尝试在控制器内访问myService 时,它为空。我不会在任何地方使用new 实例化控制器。

剪辑异常

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause:
java.lang.NullPointerException: null
at com.mypackage.controller.MyController.getOptions(MyController.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
....

编辑:

我已经从我的项目中删除了 web.xml,因为它无关紧要。

【问题讨论】:

  • 如果你有一个 Spring-boot 项目,为什么要使用 web.xml?
  • 日志错误的根本原因是什么?
  • 该参考文档不适用于 spring-boot,它适用于 spring-webmvc。 web.xml 是旧的做事方式。我认为(尽管我从未尝试将这两者混合使用)您必须声明对 spring-boot-legacy 的依赖并将 SpringBootContextLoaderListener 注册为详细的in this answer 的侦听器,以便 Spring-boot 知道您的 webxml 甚至存在。
  • @RoddyoftheFrozenPeas 感谢您指出这一点。如果 web.xml 是旧方式,那么我不想那样做。我也尝试过实现 AbstractAnnotationConfigDispatcherServletInitializer 并删除 web.xml 但结果是一样的。

标签: java spring spring-boot


【解决方案1】:

我的问题是我的控制器中的一些方法被注释为@Transactional。我没有在我的原始帖子中提供足够的信息让任何人弄清楚这一点。我在这里找到了答案:Why we shouldn't make a Spring MVC controller @Transactional?

【讨论】:

    【解决方案2】:

    Spring Java 配置:

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter{
    
        @Bean 
        BeanNameUrlHandlerMapping beanNameUrlHandlerMapping(){
            return new BeanNameUrlHandlerMapping();
        }
    }
    

    服务类:

    @Service("myService")
    public class MyServiceImpl implements MyService{
    
        @Override
        public String getMessageinfo() {
            // TODO Auto-generated method stub
            return "Hello World!!";
        }
    }
    

    服务接口:

    public interface MyService {
        public String getMessageinfo();
    }
    

    控制器类:

    @RestController
    @RequestMapping("/hello")
    public class MyController {
    
        @Autowired
        private MyService myService;
    
        @GetMapping("/getMessage")
        //@ResponseBody
        private String getMessage(HttpServletRequest req){
            System.out.println("inside controller : - "+myService.getMessageinfo());
            return myService.getMessageinfo();
        }
    }
    

    SpringbootMain

    @SpringBootApplication
    @Configuration
    @ComponentScan(basePackages={"com.example.demo.config","com.example.demo.controller","com.example.demo.service"})
    public class Demo1Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo1Application.class, args);
        }
    }
    

    这对我有用。无一例外,但我使用了 eclipse STS IDE 并创建了 Spring boot 项目。 http://localhost:8080/hello/getMessage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 2016-09-06
      • 2016-09-10
      • 2019-08-19
      相关资源
      最近更新 更多