问题:写多个接口的时候发现运行会报:
Description:
The dependencies of some of the beans in the application context form a cycle的错误
会画这种循环的小圈圈:
┌─────┐
| articleServiceImpl (field private com.blog.service.CategoryService com.blog.service.impl.ArticleServiceImpl.categoryService)
↑ ↓
| categoryService (field com.blog.service.ArticleService com.blog.service.impl.CategoryServiceImpl.articleService)
└─────┘
原因:查了一下发现是因为容器内两个类相互引用彼此,让初始化bean不知道先初始化哪一个,产生依赖循环注入
解决办法:在将对象加入IOC容器时写一个@Lazy注释,就会自动有先后顺序的进行bean初始化(注意不要加到类的上面,因为亲测类的数量超过两个还是会造成冲突)
最近还挺顺利的,就这一个问题