Field 'leaveFormService' may be 'final'

Field 'leaveFormService' may be 'final'

https://img1.sycdn.imooc.com//climg/6294377208bff95210550209.jpg

第一第二行不太理解LeaveFormService 这个class我设置的为final,为什么idea有这种提示?

代码哪里有问题,需要如何修改

package com.imooc.oa.controller;
import com.alibaba.fastjson.JSON;
import com.imooc.oa.entity.LeaveForm;
import com.imooc.oa.entity.User;
import com.imooc.oa.service.LeaveFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "LeaveFormServlet", value = "/leave/*")
public class LeaveFormServlet extends HttpServlet {
    private LeaveFormService leaveFormService = new LeaveFormService();
    private Logger logger = LoggerFactory.getLogger(LeaveFormServlet.class);
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            this.doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // http://localhost/leave/create 获取"create"
        String uri = request.getRequestURI();
        System.out.println(uri);
        String methodName = uri.substring(uri.lastIndexOf("/")+1);
        if (methodName.equals("create")){
            this.create(request,response);
        }
    }
    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException{
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("login_user");
            String formType = request.getParameter("formType");
            String strStartTime = request.getParameter("startTime");
            String strEndTime = request.getParameter("endTime");
            String reason = request.getParameter("reason");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
            Map<String,String> result = new HashMap<> ();
            try {
                LeaveForm leaveForm = new LeaveForm();
                leaveForm.setEmployeeId(user.getEmployeeId());
                leaveForm.setStartTime(sdf.parse(strStartTime));
                leaveForm.setEndTime(sdf.parse(strEndTime));
                leaveForm.setFormType(Integer.parseInt(formType));
                leaveForm.setReason(reason);
                leaveForm.setCreateTime(new Date());
                leaveFormService.createLeaveForm(leaveForm);
                result.put("code", "0");
                result.put("message", "success");
            } catch (Exception e) {
                logger.error("errorMessage",e.getMessage());
                result.put("code", e.getClass().getSimpleName());
                result.put("message", e.getMessage());
            }
            String json = JSON.toJSONString(result);
            response.getWriter().println(json);
    }
}
package com.imooc.oa.service;
import com.imooc.oa.dao.EmployeeDao;
import com.imooc.oa.dao.LeaveFormDao;
import com.imooc.oa.dao.ProcessFlowDao;
import com.imooc.oa.entity.Employee;
import com.imooc.oa.entity.LeaveForm;
import com.imooc.oa.entity.ProcessFlow;
import com.imooc.oa.service.exception.BussinessException;
import com.imooc.oa.utils.MybatilsUtils;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
//Creat a new leaveform and processflow
public class LeaveFormService {
//return db中包含主键的form对象     前端输入的表单数据
    public LeaveForm createLeaveForm(LeaveForm leaveForm){
        return  (LeaveForm)MybatilsUtils.executeUpdate(sqlSession -> {
            //持久化leaveForm level<8 -> state = processing level>=8 state = approved
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            Employee employee = employeeDao.selectByEmployeeId(leaveForm.getEmployeeId());
            if(employee.getLevel() ==8){
                leaveForm.setState("approved");
            }else{
                leaveForm.setState("processing");
            }
            //增加一条流程数据,说明表单已提交,状态为complete
            LeaveFormDao leaveFormDao = sqlSession.getMapper(LeaveFormDao.class);
            leaveFormDao.insert(leaveForm);
            ProcessFlowDao processFlowDao = sqlSession.getMapper(ProcessFlowDao.class);
            ProcessFlow flow1 = new ProcessFlow();
            flow1.setFormId(leaveForm.getFormId());
            flow1.setOperatorId(employee.getEmployeeId());
            flow1.setAction("apply");
            flow1.setCreateTime(new Date());
            flow1.setOrderNo(1);   //设置为第一个节点
            flow1.setState("complete");
            flow1.setIsLast(0);    //不是最后一个节点
            processFlowDao.insert(flow1);
            //level<7 生成部门经理审批processFlow 请假时间>36hrs 需要生成总经理审批任务
            if(employee.getLevel()<7) {
                Employee superiorLeader = employeeDao.selectLeaderByEmployee(employee);
                ProcessFlow flow2 = new ProcessFlow();
                flow2.setFormId(leaveForm.getFormId());
                flow2.setOperatorId(superiorLeader.getEmployeeId());
                flow2.setAction("audit");
                flow2.setCreateTime(new Date());
                flow2.setOrderNo(2);
                flow2.setState("process");
                long leaveTime = leaveForm.getEndTime().getTime() - leaveForm.getStartTime().getTime();
                float leaveHours = leaveTime / (1000 * 60 * 60) * 1f; // 1s = 1000ms
                if (leaveHours >= BussinessConstants.MANAGER_AUDIT_HOURS) {
                        flow2.setIsLast(0);
                        processFlowDao.insert(flow2);
                        Employee manager = employeeDao.selectLeaderByEmployee(employee);
                        ProcessFlow flow3 = new ProcessFlow();
                        flow3.setFormId(leaveForm.getEmployeeId());
                        flow3.setAction("audit");
                        flow3.setCreateTime(new Date());
                        flow3.setState("ready");
                        flow3.setOrderNo(3);
                        flow3.setIsLast(1);
                        processFlowDao.insert(flow3);
                } else {
                    //审批时间小于MANAGER_AUDIT_HOURS 直接把流程节点setIsLast=1 设置为完结,并在数据库中插入flow2
                    flow2.setIsLast(1);
                    processFlowDao.insert(flow2);
                }
            //7级员工生成总经理审批任务
            } else if (employee.getLevel() == 7) {
                   Employee manager = employeeDao.selectLeaderByEmployee(employee);
                   ProcessFlow flow = new ProcessFlow();
                   flow.setFormId(leaveForm.getFormId());
                   flow.setOperatorId(manager.getEmployeeId());
                   flow.setAction("audit");
                   flow.setCreateTime(new Date());
                   flow.setState("process");
                   flow.setOrderNo(2);
                   flow.setIsLast(1);
                   processFlowDao.insert(flow);
            //8级员工,生成总经理审批任务,系统自动通过
            }else if (employee.getLevel() == 8) {
                ProcessFlow flow = new ProcessFlow();
                flow.setFormId(leaveForm.getFormId());
                flow.setOperatorId(employee.getEmployeeId());
                flow.setAction("audit");
                flow.setReason("Auto passed");
                flow.setCreateTime(new Date());
                flow.setAuditTime(new Date());
                flow.setState("complete");
                flow.setOrderNo(2);
                flow.setIsLast(1);
                processFlowDao.insert(flow);
            }
            return leaveForm;
        });
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,这里是IDEA的警告进行不是报错可以忽略。Field 'xxx' may be 'final'表示属性xxx可以是final的,同学若不想出现此提示信息可将对应属性设置为final修饰的。

https://img1.sycdn.imooc.com//climg/629454eb09f89e7611250469.jpg

祝学习愉快~

  • 晓舟 提问者 #1

    为什么service要被设置为final,有何意义呢?

    2022-05-30 13:35:44
  • 好帮手慕小小 回复 提问者 晓舟 #2

    同学你好,由final修饰的变量其值不可以改变。将此处的成员变量设置为被final修饰,则此成员变量的值不能被修改。

    祝学习愉快~

    2022-05-30 15:16:05
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师