今天在用springboot打包成war包,准备部署到tomcat时,发现采用maven的package命令时,老是失败,报错Requested bean is currently in creation: Is there an unresolvable circular r,经过一番操作后,解决了问题。原因出在存在循环注入的问题,现在贴代码说明情况
我的项目是采用springboot、spring security,mybatis框架做成的,我在srevice层有两个实现类,分别是StuServiceImpl和FileServiceImpl,下面贴出关键代码部分
@Service
@Slf4j
public class StuServiceImp implements StuService{
@Resource
private StuMapper stuMapper;//自己写的mapper
@Resource
private UserDao userDao;//框架自带的mapper
@Resource
private RoleDao roleDao;
@Resource
private BCryptPasswordEncoder passwordEncoder;//该类是spring security框架用来给密码加密用的
@Service
public class FileServiceImpl implements FileService {
private static final Logger log = LoggerFactory.getLogger("adminLogger");
@Value("${files.path}")
private String filesPath;
@Resource
private FileInfoDao fileInfoDao;
@Resource
private StuMapper stuMapper;
@Resource
private CardMapper cardMapper;
@Resource
private UserDao userDao;
@Resource
private RoleDao roleDao;
@Resource
private BCryptPasswordEncoder passwordEncoder;
从以上可以看到,这两个实现类都注入了BCryptPasswordEncoder,导致了打包失败
解决办法是加@Lazy,如下图所示:
@Resource @Lazy private BCryptPasswordEncoder passwordEncoder;
本文介绍了一种在Spring Boot项目中遇到的循环依赖问题及解决方案。通过使用@Lazy注解来避免因两个服务类互相注入同一组件导致的打包失败。
2075

被折叠的 条评论
为什么被折叠?



