AnnotationConfigApplicationContext ad has not been refreshed yet 错误

在使用spring框架使用IOC获取bean时有时可能会碰到这个错误

AnnotationConfigApplicationContext  ad has not been refreshed yet

看起来似乎是annotationConfigApplicationContext未刷新的问题,但事实并不是,在添加annotationConfigApplicationContext.refresh();语句后将会出现新的错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'defaultCache' available

其实这个问题的本质是beans配置文件并未加载,导致annotationConfigApplicationContext无法获取任何bean信息

 

 

解决方法就是在annotationConfigApplicationContext声明时添加配置文件,

AnnotationConfigApplicationContext annotationConfigApplicationContext= new AnnotationConfigApplicationContext(SpringConfig.class);

注意是添加Class,即配置类对象

如果是XML方式,则在ClassPathXMLApplicationContext的实例添加XML配置文件即可

 

 

 

 

 

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务