问题描述:使用@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,是否注入;

如何解决的问题:

sping加载bean都发生了些什么

 

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

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

总结spring加载bean的理解:

相关文章:

  • 2022-02-16
  • 2021-11-07
  • 2021-10-23
  • 2022-02-08
  • 2022-12-23
  • 2019-08-27
  • 2021-04-15
猜你喜欢
  • 2022-12-23
  • 2021-06-05
  • 2021-07-31
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-11-16
相关资源
相似解决方案