代码如下(为了演示这个报错):
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
("xxx");
报错如下:
Exception in thread "main" : @20ad9418 has not been refreshed yet
at (:1032)
at (:1044)
at com.(:18)
在使用spring框架使用IOC获取bean时有时可能会碰到这个错误
AnnotationConfigApplicationContext ad has not been refreshed yet
看起来似乎是annotationConfigApplicationContext未刷新的问题,但事实并不是,在添加();语句后将会出现新的错误
: No bean named 'defaultCache' available
其实这个问题的本质是beans配置文件并未加载,导致annotationConfigApplicationContext无法获取任何bean信息
解决方法就是在annotationConfigApplicationContext声明时添加配置文件,
AnnotationConfigApplicationContext annotationConfigApplicationContext= new AnnotationConfigApplicationContext();
或者将配置类注册到AnnotationConfigApplicationContext中:
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
();
();
注意是添加Class,即配置类对象
如果是XML方式,则在ClassPathXMLApplicationContext的实例添加XML配置文件即可
欢迎大家私信博主,邀你进技术交流群