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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章