AnnotationConfigApplicationContext@xxxx has not been refreshed yet

使用註解方式來配置JavaConfig類,從而代替XML配置文件時運行報錯:
AnnotationConfigApplicationContext@1517365b has not been refreshed yet
原因

  1. 需要使用註解類配置文件專用的加載類:
    AnnotationConfigApplicationContext
  2. 檢查創建加載類的時候是否傳遞了目標配置類的class:
    PersonConfig.class

正確寫法如下:

@Test
public void test(){
    AnnotationConfigApplicationContext ac = 	// 注意格式和類名
    		new AnnotationConfigApplicationContext(PersonConfig.class);
    Person person = ac.getBean("person", Person.class);
    System.out.println(person);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章