java.lang.IllegalStateException:Autowired annotation需要至少一个参数

时间:2014-05-15 15:59:08

标签: java spring

无法自动装配和运行Spring Web应用程序。

错误:

java.lang.IllegalStateException: Autowired annotation requires at least one argument: public main.java.com.springapp.mvc.controller.DSLRServletController()

DSLRServletController:

package main.java.com.springapp.mvc.controller;

import main.java.com.springapp.mvc.dao.DSLRDAO;
import main.java.com.springapp.mvc.model.DSLR;
import main.java.com.springapp.mvc.pckg.DSLRForm;
import main.java.com.springapp.mvc.pckg.DSLRValidaor;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

@Controller
public class DSLRServletController   {
    static Logger logger = Logger.getLogger(DSLRServletController.class);


    private DSLR DSLR;
    private DSLRDAO dslrDAO;
    private DSLR dslr;



    @Autowired
    public DSLRServletController() {
        this.dslrDAO = new DSLRDAO();
    }

    public void init() {logger.error("DSLRServlet.init(): just started"); }

@RequestMapping(value = "/s", method = RequestMethod.GET)
public String showHTMLResponse(@ModelAttribute("dslrs") DSLR dslrs[],
                               @ModelAttribute("dslr") DSLR dslr,
                               @ModelAttribute("dslrErrors") HashMap dslrErrors,
                               @ModelAttribute ("dslrform") DSLRForm dslrForm,
                               @RequestParam("id") String paramId,
                               @RequestParam("action") String paramAction,
                               Model model){

    if(paramId == null || paramId.equals("")){
        //show_all_dslrs
        dslrs = getAllDslrs();    // DSLR adslrs[] -> to MODEL; HOW?
       return "dslrs";
    }else{
        //show_this_dslr;
        HashMap<String,Object> dslrHashMap = getDSLRById(paramId);
        dslr = (DSLR) dslrHashMap.get("dslr");
        dslrForm = (DSLRForm)dslrHashMap.get("dslrForm");
        dslrErrors = (HashMap)dslrHashMap.get("dslrErrors");

            if(dslr != null){

                return "dslr";
            }

            else{
                return "error";
            }


    }
}

@RequestMapping(value = "/s", method = RequestMethod.POST)
public String showHTMLResponsePOST(@ModelAttribute("dslrs") DSLR dslrs[],
                               @ModelAttribute("dslrErrors") HashMap<?,?> dslrErrors,
                               @ModelAttribute ("dslrform") DSLRForm dslrForm,

                               @RequestParam("id") String paramId,
                               @RequestParam("action") String paramAction,
                               @RequestParam("dslr_model") String paramModel,
                               @RequestParam("price") String paramPrice,
                               @RequestParam("description") String paramDescription,
                               Model model){
    int iStatusCode = 0;
    if(paramAction.equals("save") )
        iStatusCode = saveDSLR(paramId, paramModel, paramPrice, paramDescription, dslrErrors, dslrForm);    // POST

    return "dslrs";

}

    private int saveDSLR(String paramId,
                         String paramModel,
                         String paramPrice,
                         String paramDescription,
                         HashMap<?,?> context_dslrErrors,
                         DSLRForm context_dslrForm
                         ) {
        int byte0 = 1;
        try {

            DSLRValidaor dslrValidaor = new DSLRValidaor();

            DSLRForm dslrForm = new DSLRForm();
            dslrForm.setDslrId(paramId);
            dslrForm.setModel(paramModel);
            dslrForm.setPrice(paramPrice);
            dslrForm.setDescription(paramDescription);

            HashMap hashmap = dslrValidaor.Validate(dslrForm);
            if(hashmap.size() > 0) {
                context_dslrForm = dslrForm;
                context_dslrErrors = hashmap;
                byte0 = -1;
            } else{
                DSLRDAO planedao = new DSLRDAO();
                DSLR dslr = new DSLR();
                dslr.setDslrId(Integer.parseInt(paramId));
                dslr.setModel(paramModel);
                dslr.setPrice(Integer.parseInt(paramPrice));
                dslr.setDescription(paramDescription);
                planedao.update(dslr);
            }
        }
        catch(Exception exception)
        {
            logger.error((new StringBuilder()).append("DSLRServlet.saveDSLR():").append(exception.getMessage()).toString());
            byte0 = -1;
        }
        return byte0;

    }

    private DSLR[] getAllDslrs(){
        DSLR adslrs[] = null;
        try
        {
            DSLRDAO DSLRDAO = new DSLRDAO();
            adslrs = (DSLR[])DSLRDAO.findAll();
        }
        catch(Exception exception)
        {
            logger.error((new StringBuilder()).append("PlaneServlet.getAllPlanes():").append(exception.getMessage()).toString());
        }
//        request.setAttribute("dslrs", adslrs);
        return adslrs;
    }

    private HashMap<String, Object> getDSLRById(String s)
    {
        HashMap<String,Object> map = new HashMap<String, Object>();

        DSLR dslr = null;
        try {
            int i = Integer.parseInt(s);
            DSLRDAO DSLRDAO = new DSLRDAO();
            dslr = (DSLR)DSLRDAO.findById(i);

            DSLRForm dslrForm = new DSLRForm();
            dslrForm.setDslrId(Integer.toString(dslr.getDslrId()));
            dslrForm.setModel(dslr.getModel());
            dslrForm.setPrice(Integer.toString(dslr.getPrice()));
            dslrForm.setDescription(dslr.getDescription());


            map.put("dslr", dslr);
            map.put("dslrform", dslrForm);
            map.put("dslrErrors", new HashMap());
        }
        catch(Exception exception)
        {
            logger.error((new StringBuilder()).append("DSLRServlet.getDSLRById():").append(exception.getMessage()).toString());
        }
        return map;
    }
    @Autowired
    public void setDslrDAO(DSLRDAO dslrDAO) {
        this.dslrDAO = dslrDAO;
    }

    public DSLRDAO getDslrDAO() {
        return dslrDAO;
    }
    @Autowired
    public void setDSLR(DSLR DSLR) {
        dslr = DSLR;
    }

    public DSLR getDSLR() {
        return dslr;
    }
}

为什么@Autowired注释会返回错误?如何解决?

2 个答案:

答案 0 :(得分:9)

除了reimeus回答,你不能在默认构造函数上使用@Autowired。

通常认为自动装配构造函数而不是字段更好。

@Autowired
public DSLRServletController(DSLRDAO dslrDAO) {
    this.dslrDAO = dslrDAO;
}

答案 1 :(得分:4)

构造函数不是有效的setter方法或实例变量

@Autowired
private DSLRDAO dslrDAO;