sping加载bean都发生了些什么

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

//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都有注入app

解决问题的过程:spa

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

如何解决的问题:code

 

反思问题产生的缘由:不是单例,又从新new了一个对象;xml

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

总结spring加载bean的理解:blog

相关文章
相关标签/搜索