使用註解方式來配置JavaConfig類,從而代替XML配置文件時運行報錯:
AnnotationConfigApplicationContext@1517365b has not been refreshed yet
原因
- 需要使用註解類配置文件專用的加載類:
AnnotationConfigApplicationContext - 檢查創建加載類的時候是否傳遞了目標配置類的class:
PersonConfig.class
正確寫法如下:
@Test
public void test(){
AnnotationConfigApplicationContext ac = // 注意格式和類名
new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = ac.getBean("person", Person.class);
System.out.println(person);
}