zoukankan      html  css  js  c++  java
  • sping加载bean都发生了些什么

    问题描述:使用@Autowired注入的类,没有实例化

    //Controller
    @RequestMapping(value="/deepblue")
    @Controller
    public class AController{
    
        @Autowired
        private BService bService;
    
        public void test(){
            bService.test();
        }    
    }
    
    //Service
    @Service
    public class BService{
        public void test(){
            new CService().test();   
        }
    }
    
    //Service
    @Component
    public class CService{
        @Autowired
        private DataService dataService;  // null
    
        @Value(${data.service.ticket})
        private String dataServiceTicket;  // null
    }

    上述代码debug,发现@Autowired @Value注入为null,依次检查application.properties和dubbo-spring.xml文件,@Value变量和dubbo bean都有注入

    解决问题的过程:

    解决问题都过程放在spring容器启动都时候是否扫描类DataService,是否注入;

    如何解决的问题:

    反思问题产生的原因:不是单例,又重新new了一个对象;

    You should autowire your PersonService class in your controller instead of making it as an object

    总结spring加载bean的理解:

  • 相关阅读:
    RabbitMQ
    连接池,为什么要使用连接池?
    mac 安装arcanist
    感触
    UDP socket
    Servlet过滤器
    PL、SQL
    springmvc 文件上传实现(不是服务器的)
    注解spring
    excel工具类
  • 原文地址:https://www.cnblogs.com/RunForLove/p/6568327.html
Copyright © 2011-2022 走看看