Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我所有的@Autowired bean都是空的_Java_Spring_Spring Boot - Fatal编程技术网

Java 我所有的@Autowired bean都是空的

Java 我所有的@Autowired bean都是空的,java,spring,spring-boot,Java,Spring,Spring Boot,我不明白为什么我所有的bean在控制器中都是空的。我知道这是一个常见的问题,但我不是用new实例化对象 控制器: - Processing injected element of bean 'myController': AutowiredFieldElement for private com.mypackage.service.MyService com.mypackage.controller.MyController.myService - Returning cached instan

我不明白为什么我所有的bean在控制器中都是空的。我知道这是一个常见的问题,但我不是用new实例化对象

控制器:

- 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'
当我要求时http://localhost/store/getOptions 我在myService上得到一个NullPointerException

我的服务

网络配置

主要

当我启动应用程序时,我可以在日志中看到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实例化控制器

裁剪异常

编辑:


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

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();
    }
}
春靴

@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);
    }
}
这对我有用。毫无例外,但我使用了EclipseSTIDE并创建了SpringBoot项目。

我的问题是控制器中的一些方法被注释为@Transactional。我没有在我的原始帖子中提供足够的信息让任何人都明白这一点。我在这里找到了答案:

如果你有一个Spring boot项目,为什么要使用web.xml?我使用web.xml是因为日志错误的根本原因是什么?该参考文档不适用于Spring boot,它适用于Spring webmvc。xml是做事情的老方法。我认为,尽管我从未尝试将这两种方法混合使用,但您必须声明对spring boot legacy的依赖关系,并将SpringBootContextLoaderListener注册为详细的侦听器,以便spring boot知道您的webxml甚至存在。@RoddyOfFrozenpeas感谢您指出这一点。如果web.xml是旧方法,那么我不想这样做。我还尝试过实现AbstractAnnotationConfigDispatcherServletializer并删除web.xml,但结果是一样的。
@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();
    }
}
@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);
    }
}