spring 注入servcie时循环引用解决


@Service
@Transactional(readOnly = true)
public class ReportRecordService extends CrudService<ReportRecordDao, ReportRecord> {
    private SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Pattern num = Pattern.compile("^([\u4E00\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D\u5341]+)|(\\d+)\\.00|(\\(\\d+\\))$");
    @Autowired
    private ServletContext servletContext;
    @Resource
    private ReportRecordDao reportRecordDao;
    @Resource
    private DailySingleDepartReportDao dailySingleDepartReportDao;
    @Resource
    private MonthlySingleDepartReportDao monthlySingleDepartReportDao;
    @Resource
    private ReportAuditLogService auditLogService;
    @Resource
    private DailyReportIndexInfoDao dailyReportIndexInfoDao;
    @Resource
    private MonthlyReportIndexInfoDao monthlyReportIndexInfoDao;
    @Resource
    private UserDao userDao;
    @Resource
    private OfficeService officeService;
    @Resource
    private InnerMsgService innerMsgService;
    @Autowired
    @Lazy(true)
    private MonthEffectiveDataUpdateService monthEffectiveDataUpdateService;
  .......


  
/**
* 审核报表
*
* @param id 报表的编号
* @param auditState 报表的审核状态[通过,不通过]
* @param auditorRemarks 审核备注
* @param msgId 任务编号,如果没有请填写'0'
* @return 审核的报表
* @throws Exception 异常信息
*/
@Transactional()
public Message audit(String id, String auditState, String auditorRemarks, Integer msgId, final User user) throws Exception {
Message message = null;
ReportRecord reportRecord = get(id);
ReportAuditLog auditLog = new ReportAuditLog(); // 审核日志对象
/* 如果存在该报表,则设置审核属性,执行审核流程*/
if (reportRecord != null) {
reportRecord.setAuditNode(user.getOffice().getGrade());
reportRecord.setDataStatus(DataStatus.DRAFT);
if (AuditStatus.APPROVED.equals(auditState)) {
String parentNode = UserCache.getParentAuditNode(Integer.parseInt(reportRecord.getAuditNode()), user);
if (StringUtils.isEmpty(parentNode)) {
reportRecord.setAuditStatus(auditState);
reportRecord.setDataStatus(DataStatus.EFFECTIVE);
}
} else if (AuditStatus.NOT_APPROVED.equals(auditState)) {
reportRecord.setAuditStatus(auditState);
}
reportRecord.setAuditorRemarks(auditorRemarks);
reportRecord.setAuditDate(new Date());
/* 设置审核人信息 */
reportRecord.setAuditorId(user.getId());
reportRecord.setAuditorName(user.getName());
/* 如果是通过,则数据生效,否则变为草稿状态*/
if (ReportConstants.REPORT_RECORD_CHECK_STATE_TG.equals(auditState)) {
auditLog.setOperates(ReportConstants.REPORT_RECORD_CHECK_STATE_TG);
} else {
auditLog.setOperates(ReportConstants.REPORT_RECORD_CHECK_STATE_WTG);
}
this.update(reportRecord);
// 如果审核通过则,数据立即生效
if (reportRecord.getAuditStatus().equals(ReportConstants.REPORT_RECORD_CHECK_STATE_TG)) {
//exportData(reportRecord.getId());
makeReportRecordEffictive(reportRecord.getId());
new Thread(() -> {
//数据生效则立即调用月报的生效数据更新接口进行月报的数据刷新服务
List<MonthlySingleDepartReport> monthlySingleDepartReportsCache = null;
String monthReportCacheKey = ReportCalculateUpdateUtils.buildRedisUpdateDataKey(ReportCalculateUpdateUtils.SUB_OFFICE_MONTHLY_REPORT, reportRecord.getReportDate(),
reportRecord.getCompanyId(), ReportConstants.MONTHLY_REPORT_SINGLE_TYPE);
monthlySingleDepartReportsCache = (List<MonthlySingleDepartReport>) JedisUtils.getObject(monthReportCacheKey);
if (monthlySingleDepartReportsCache == null || monthlySingleDepartReportsCache.size() <= 0) {
MonthlySingleDepartReport mQuery = new MonthlySingleDepartReport();
mQuery.setCompanyNo(reportRecord.getCompanyId());
mQuery.setPage(new Page<>(1, -1));//手动设置不分页
mQuery.setDateTime(DateUtils.parseDate(reportRecord.getReportDate()));
monthlySingleDepartReportsCache = monthlySingleDepartReportDao.findList(mQuery);
}
monthEffectiveDataUpdateService.updateEffectiveMonthSingleReportDatas(DateUtils.formatDate(reportRecord.getReportDate(), "yyyy-MM"),
reportRecord.getCompanyId(), monthlySingleDepartReportsCache);
}).start();
}
if (msgId == null || msgId == 0) {
msgId = innerMsgService.getMsgByReportIdAndReceiver(reportRecord.getId(), user.getId());
}
final Integer newMsgId = msgId;
new Thread(() -> {
try {
//根新任务状态
if (newMsgId != null && newMsgId > 0) {
innerMsgService.taskFinished(newMsgId);
}
String reportName = reportRecord.getReportName();
if (AuditStatus.APPROVED.equals(auditState)) {

if (DataStatus.EFFECTIVE.equals(reportRecord.getDataStatus())) {//最后通过
List<User> officeUserList = userDao.findUserListByOfficeId(user.getOffice().getId().toString());
/* 上级部门所有用户数据*/
//TODO 此处目前只获取了上级部门的运营部人员,若需获取总公司所有人员,重新赋值此集合即可!
List<User> userByOfficeId = InnerMsgUtils.getParentDepartmentUser();
userByOfficeId.addAll(officeUserList);
List<String> tmp = Lists.newArrayList();
userByOfficeId.forEach(u -> tmp.add(u.getId()));
InnerMsgUtils.monthFinalAuditPass(user.getId(), reportName, tmp, reportRecord.getId());
} else {//中间通过
List<String> childrenList = UserCache.getParentAuditNodeIds(Integer.parseInt(reportRecord.getAuditNode()), user);
InnerMsgUtils.multipleAudit(user.getId(), reportName, childrenList, MessageConstants.MONTHLY_REPORT_UNAUDIT, reportRecord.getId());
}

} else {//不通过发送到录入员
List<String> childrenList = InnerMsgUtils.getReceiverByGroup(MessageConstants.USER_INPUT, reportRecord.getCompanyId());
InnerMsgUtils.multipleAudit(user.getId(), reportName, childrenList, MessageConstants.MONTHLY_REPORT_AUDIT_REFUSED, reportRecord.getId());
}

} catch (Exception e) {
e.printStackTrace();
}
}).start();

message = Message.createMessage().setReturnData(reportRecord);
} else {
// 确保返回的结果不会为null
message = Message.createMessage("Not find a report record which id = " + id);
message.setReturnData(new ReportRecord());
}
/* 将此次操作记录到审核日志表中 */
auditLog.setUserName(user.getName());
auditLog.setAuditDate(new Date());
auditLog.setReportId(Integer.parseInt(id));
auditLog.setRemarks(auditorRemarks);
auditLog.setCreateBy(user);
auditLogService.save(auditLog);
return message;
}

}

最近在设置spring注入service时,发现出现了循环的引用,启动时报错提示循环引用的问题。最后只需要在注入的service身上添加@Lazy(true)启动解决了这个错误。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM