@autowire注释不起作用

时间:2015-01-20 07:27:10

标签: java hibernate spring-mvc

我有一个控制器GGSNAcceptController.java:

package com.viettel.pcrf.controller;

import com.viettel.fw.Exception.LogicException;
import com.viettel.fw.dto.BaseMessage;
import com.viettel.fw.web.controller.BaseController;
import com.viettel.pcrf.common.Const;
import com.viettel.pcrf.dto.GgsnAcceptDTO;
import com.viettel.pcrf.webconfig.service.GgsnAcceptService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@ManagedBean(name = "ggsnAcceptController")
@Scope("view")
public class GGSNAcceptController extends BaseController implements Serializable, BaseTableCTRL {

    /**
     * VARIABLES & GETTER/SETTER
     */
    private GgsnAcceptDTO ggsnAccept;
    private GgsnAcceptDTO selectedGgsnAccept;
    private List<GgsnAcceptDTO> listGgsnAccept;

    public GgsnAcceptDTO getGgsnAccept() {
        return ggsnAccept;
    }

    public void setGgsnAccept(GgsnAcceptDTO ggsnAccept) {
        this.ggsnAccept = ggsnAccept;
    }

    public GgsnAcceptDTO getSelectedGgsnAccept() {
        return selectedGgsnAccept;
    }

    public void setSelectedGgsnAccept(GgsnAcceptDTO selectedGgsnAccept) {
        this.selectedGgsnAccept = selectedGgsnAccept;
    }

    public List<GgsnAcceptDTO> getListGgsnAccept() {
        return listGgsnAccept;
    }

    public void setListGgsnAccept(List<GgsnAcceptDTO> listGgsnAccept) {
        this.listGgsnAccept = listGgsnAccept;
    }

    /**
     * SERVICE
     *
     */

    @Autowired
    private GgsnAcceptService ggsnAcceptServ;

    /**
     * INIT
     *
     */

    @PostConstruct
    @Override
    public void init() {
        updateCtrl();
    }

    @Override
    public void updateCtrl() {
        clear();
        System.out.println(ggsnAcceptServ == null);
        listGgsnAccept = ggsnAcceptServ.findAll();
    }

    private String ggsnAcceptSelected;

    @Override
    public void updateDB() {
        try {
            if (ggsnAccept == null) {
                throw new LogicException("nullSelected", "GGSN Config is not yet selected!");
            }

            if (formStatus == Const.BTN_ADD && ggsnAcceptServ.isExisted(ggsnAccept)) {
                throw new LogicException("insertExisted", "GGSN Config existed!");
            }

//          if (systemCfgSelected != null && systemCfgSelected.equals(systemCfg.getSystemCfgName()) && langServ.isExisted(systemCfg)) {
//              throw new LogicException("updateExisted", "GGSN Config is existed!");
//          }
            BaseMessage msg = ggsnAcceptServ.updateGgsn(ggsnAccept);

            if (msg.isSuccess()) {
                reportSuccess("msgInfo", "Success");
            }

            updateCtrl();
            selectedGgsnAccept = (GgsnAcceptDTO) msg.getOutputObject();

        } catch (LogicException ex) {
            reportError("msgInfo", ex.getDescription());
        } catch (Exception ex) {
            logger.error(ex, ex);
        }
    }

    @Override
    public void deleteDB() {
        try {
            if (ggsnAccept == null) {
                throw new LogicException("nullSelected", "GGSN Config is not selected yet!");
            }

            BaseMessage msg = ggsnAcceptServ.deleteGgsn(ggsnAccept);

            if (msg.isSuccess()) {
                reportSuccess("msgInfo", "msg.delete.success");
            }

            updateCtrl();

        } catch (LogicException ex) {
            reportError("msgInfo", ex.getDescription());
        } catch (Exception ex) {
            logger.error(ex, ex);
        }
    }

    @Override
    public void prepareAdd() {
        ggsnAccept = new GgsnAcceptDTO();
        selectedGgsnAccept = null;
    }

    @Override
    public void prepareEdit() {
        if (selectedGgsnAccept != null) {
            ggsnAccept = selectedGgsnAccept;
        }
    }

    @Override
    public void prepareDelete() {
        if (selectedGgsnAccept != null) {
            ggsnAccept = selectedGgsnAccept;
        }
    }

    @Override
    public void clear() {
        selectedGgsnAccept = null;
        ggsnAccept = null;
    }

    @Override
    public void onRowChangeListener() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

接口GgsnAcceptService.java:

package com.viettel.pcrf.webconfig.service;

import com.viettel.fw.dto.BaseMessage;
import com.viettel.pcrf.dto.GgsnAcceptDTO;
import java.util.List;

public interface GgsnAcceptService {

    public List<GgsnAcceptDTO> findAll();

    public List<GgsnAcceptDTO> findAll(List filters);

    public BaseMessage updateGgsn(GgsnAcceptDTO ggsn) throws Exception;

    public BaseMessage deleteGgsn(GgsnAcceptDTO ggsn) throws Exception;

    public boolean isExisted(GgsnAcceptDTO ggsn) throws Exception;
}

以上接口的类实现:

package com.viettel.pcrf.webconfig.service;

import com.viettel.fw.common.util.extjs.FilterRequest;
import com.viettel.fw.dto.BaseMessage;
import com.viettel.pcrf.webconfig.repo.GgsnAcceptRepository;
import com.viettel.pcrf.common.util.mapper.GgsnAcceptMapper;
import com.viettel.pcrf.dto.GgsnAcceptDTO;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.viettel.service.BaseServiceImpl;
import java.util.ArrayList;

@Service
public class GgsnAcceptServiceImpl extends BaseServiceImpl implements GgsnAcceptService {

    private GgsnAcceptMapper mapper = new GgsnAcceptMapper();

    @Autowired
    private GgsnAcceptRepository repository;

    public Logger logger = Logger.getLogger(GgsnAcceptService.class);

    @Override
    public List<GgsnAcceptDTO> findAll(List filters) {
        return mapper.toDtoBean(repository.findAll(repository.toPredicate(filters)));
    }

    @Override
    public List<GgsnAcceptDTO> findAll() {
        return mapper.toDtoBean(repository.findAll());
    }

    @Override
    public BaseMessage updateGgsn(GgsnAcceptDTO ggsn) throws Exception {
        BaseMessage msg = new BaseMessage();
        GgsnAcceptDTO newGgsn = mapper.toDtoBean(repository.saveAndFlush(mapper.toPersistenceBean(ggsn)));
        msg.setOutputObject(newGgsn);
        msg.setSuccess(true);
        return msg;
    }

    @Override
    public boolean isExisted(GgsnAcceptDTO ggsn) throws Exception {
        List<FilterRequest> listReq = new ArrayList<>();
        listReq.add(new FilterRequest("IP", ggsn.getIp()));
        return repository.findOne(repository.toPredicate(listReq)) != null;
    }

    @Override
    public BaseMessage deleteGgsn(GgsnAcceptDTO ggsn) throws Exception {
        BaseMessage msg = new BaseMessage();
        repository.delete(mapper.toPersistenceBean(ggsn));
        msg.setSuccess(true);
        return msg;
    }

}

尝试访问页面调用控制器时出现空错误。我的代码有什么问题吗? 我的属性

ggsnAcceptServ
总是为null,尽管我已经为它设置了自动装配。我是Spring的新手,请帮助解释为什么这个属性为null。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

混淆jsf和spring时遇到问题:

@Component
@ManagedBean(name = "ggsnAcceptController")
@Scope("view")

您的控制器将在jsf上下文中执行,而不是在spring上下文中执行。这就是为什么autowiering不起作用。