我所有的@Autowired bean都是null

时间:2018-01-30 23:09:25

标签: java spring spring-boot

我无法弄清楚为什么我的所有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。

为MyService

@Service
public class MyService
{
    ....
}

WebConfig

@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,因为它无关紧要。

2 个答案:

答案 0 :(得分:0)

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启动项目。 http://localhost:8080/hello/getMessage

答案 1 :(得分:0)

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

相关问题