SpringBoot我想把服务层一个功能抽成工具类,但是一直保存null
首先我希望是一个静态的方法,但是earlyWarningService会变红
然后我把static去掉,查询为null
package com.bulldozer.utils;
import com.bulldozer.entity.EarlyWarning;
import com.bulldozer.service.EarlyWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AbnormalTaskUtil {
@Autowired
private EarlyWarningService earlyWarningService;
public void static beginEarlyWarning() {
List<EarlyWarning> listEarlyWarning = earlyWarningService.selectEarlyWarningCount("ASC");
for(EarlyWarning earlyWarningCount: listEarlyWarning) {
// int countExisted = earlyWarningService.selectEarlyWarningIsExisted(earlyWarning.getLicenseNumber());
List<EarlyWarning> listEarlyWarningSelected = earlyWarningService.selectEarlyWarningByLicenseNumber(earlyWarningCount.getLicenseNumber());
if(listEarlyWarningSelected.size() == 0) {
if(earlyWarningCount.getEntryCount() != earlyWarningCount.getLeaveCount()) {
earlyWarningCount.setWarningType("出入次数异常");
} else {
// earlyWarning.setWarningType("");
earlyWarningCount.setFlagDelete(1);
}
System.out.println(earlyWarningCount.toString());
earlyWarningService.insertEarlyWarning(earlyWarningCount);
} else {
EarlyWarning earlyWarningSelected = listEarlyWarningSelected.get(0);
if(earlyWarningCount.getLeaveCount() != earlyWarningCount.getEntryCount()) {
// earlyWarningSelected.get(0).setFlagDelete(1);
earlyWarningSelected.setLeaveCount(earlyWarningCount.getLeaveCount());
earlyWarningSelected.setEntryCount(earlyWarningCount.getEntryCount());
earlyWarningSelected.setWarningType("出入次数异常");
earlyWarningSelected.setFlagDelete(0);
if(earlyWarningSelected.getFlagDelete() == 0) {
} else {
earlyWarningSelected.setWarningTimeStr("current");
}
System.out.println("异常earlyWarningCount == " + earlyWarningCount.toString());
System.out.println("异常earlyWarningSelected == " + earlyWarningSelected.toString());
} else {
// System.out.println("正常的earlyWarningCount == " + earlyWarningCount.toString());
earlyWarningSelected.setFlagDelete(1);
}
earlyWarningService.updateEarlyWarningByIdAndLicenseNumber(earlyWarningSelected);
}
}
}
}
外加问一下
我一个接口和一个定时器都需要使用这个方法
请问这个方法我怎么抽出来共用
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老哥你这基础,讲道理 真的拉跨 - -
静态方法你使用非静态变量,能不报错嘛?这是第一个
如果autowrite修饰的是静态类的时候,是注入不成功的!直接将autowrite注解写到set方法上面,就会给你注入了,不过推荐自己去实现ApplicationContextAware,这样你都能在非容器管理的不同类,获取到applicationcontext,简介获取到所有的bean