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

相關文章
相關標籤/搜索