1

好吧,我有一个像这样的抽象类:

public abstract class BasicCrudMBImpl<Bean, BO> extends BasicMBImpl {

    protected Bean bean;
    protected List<Bean> beans; 
    protected BO boPadrao;

    public void deletar() {
        try {
            ((BasicBO) boPadrao).delete((AbstractBean) bean);
            addInfoMessage("Registro deletado com sucesso");
            beans = retornaBeansDoBanco();
            bean = null;            
        } catch (BOException e) {
            addErrorMessage(e.getMessage());
        }
    }

    public void salvar(ActionEvent event) {
        try {

            if (((AbstractBean) bean).getId() == null) {
                bean = (Bean) ((BasicBO) boPadrao).save((AbstractBean) bean);
                addInfoMessage("Registro salvo com sucesso");
            } else {
                ((BasicBO) boPadrao).update((AbstractBean) bean);
                addInfoMessage("Registro atualizado com sucesso");
            }

            beans = retornaBeansDoBanco();
        } catch (BOException e) {
            FacesContext.getCurrentInstance().validationFailed();
            addErrorMessage(e.getMessage());
        }
    }

    public Bean getBean() {
        return bean;
    }

    public void setBean(Bean bean) {
        this.bean = bean;
    }

    public List<Bean> getBeans() {
        try {
            if (beans == null)
                beans = (List<Bean>) retornaBeansDoBanco();
            return beans;
        } catch (BOException e) {
            addErrorMessage(e.getMessage());
        }
        return null;
    }

    public void setBeans(List<Bean> beans) {
        this.beans = beans;
    }   

    // Deve ser implementado para carregar a query adequada ao bean necessário
    public abstract List<Bean> retornaBeansDoBanco();

    public abstract void novo(ActionEvent event);

    public abstract void alterar(ActionEvent event);

    public BO getBoPadrao() {
        return boPadrao;
    }

    public abstract void setBoPadrao(BO boPadrao);

    public void addErrorMessage(String componentId, String errorMessage) {
        addMessage(componentId, errorMessage, FacesMessage.SEVERITY_ERROR);
    }

    public void addErrorMessage(String errorMessage) {
        addErrorMessage(null, errorMessage);
    }

    public void addInfoMessage(String componentId, String infoMessage) {
        addMessage(componentId, infoMessage, FacesMessage.SEVERITY_INFO);
    }

    public void addInfoMessage(String infoMessage) {
        addInfoMessage(null, infoMessage);
    }

    private void addMessage(String componentId, String errorMessage,
            FacesMessage.Severity severity) {
        FacesMessage message = new FacesMessage(errorMessage);
        message.setSeverity(severity);
        FacesContext.getCurrentInstance().addMessage(componentId, message);
    }
}

在 ManagedBean 中,我尝试用 注入“boPadrao” @Autowired,如下所示:

@ManagedBean(name = "enderecoMB")
@ViewScoped
public class EnderecoMBImpl extends BasicCrudMBImpl<Endereco, BasicBO> {

    private static Logger logger = Logger.getLogger(EnderecoMBImpl.class);

    private List<TipoEndereco> tiposEndereco;
    private List<Logradouro> logradouros;

    @PostConstruct
    public void init() {
        logger.debug("Inicializando componentes no PostConstruct");
        beans = retornaBeansDoBanco();
        tiposEndereco = (List<TipoEndereco>) boPadrao
                .findByNamedQuery(TipoEndereco.FIND_ALL);
        logradouros = (List<Logradouro>) boPadrao
                .findByNamedQuery(Logradouro.FIND_ALL_COMPLETO);
    }

    @Override
    public List<Endereco> retornaBeansDoBanco() {
        return (List<Endereco>) getBoPadrao().findByNamedQuery(Endereco.FIND_ALL_COMPLETO);
    }

    @Override
    public void novo(ActionEvent event) {
        bean = new Endereco();
    }

    @Override
    public void alterar(ActionEvent event) {
        // TODO Auto-generated method stub

    }



    public List<TipoEndereco> getTiposEndereco() {
        return tiposEndereco;
    }

    public void setTiposEndereco(List<TipoEndereco> tiposEndereco) {
        this.tiposEndereco = tiposEndereco;
    }

    public List<Logradouro> getLogradouros() {
        return logradouros;
    }

    public void setLogradouros(List<Logradouro> logradouros) {
        this.logradouros = logradouros;
    }

    @Autowired
    public void setBoPadrao(BasicBO boPadrao) {
        this.boPadrao = boPadrao;       
    }



}

但这不起作用,boPadrao 总是null,得到一个“NullPointerException”。错误发生在方法 retornaBeansDoBanco();

4

0 回答 0