自动装配的字段为空

时间:2016-12-23 20:33:33

标签: java spring nullpointerexception java-8 autowired

我正在努力使通用的表单设计。其中一个组件是一个类,它是Panel的详细信息......

package net.draconia.test.ui;

import java.awt.BorderLayout;
import java.awt.LayoutManager;

import java.util.Observable;

import javax.annotation.PostConstruct;

import javax.swing.Action;
import javax.swing.JPanel;

import net.draconia.test.ui.model.DetailsPanelModel;

import org.springframework.beans.factory.annotation.Autowired;

public abstract class DetailsPanel<ModelType extends Observable> extends EnablablePanel
{
    private static final long serialVersionUID = -9083062930871602527L;

    private Action[] mArrButtonActions;
    private ButtonsPanel mPnlButtons;

    @Autowired
    private DetailsPanelModel<ModelType> mObjModel;

    public DetailsPanel(final LayoutManager objLayoutManager)
    {
        this(objLayoutManager, null, null);
    }

    public DetailsPanel(final LayoutManager objLayoutManager, final Action[] arrButtonActions)
    {
        this(objLayoutManager, arrButtonActions, null);
    }

    public DetailsPanel(final LayoutManager objLayoutManager, final Action[] arrButtonActions, final ModelType objModel)
    {
        super(objLayoutManager);

        setButtonActions(arrButtonActions);

        getModel().setModel(objModel);
    }

    protected Action[] getButtonActions()
    {
        return(mArrButtonActions);
    }

    protected ButtonsPanel getButtonsPanel()
    {
        return(mPnlButtons);
    }

    protected abstract JPanel getFieldsPanel();

    public DetailsPanelModel<ModelType> getModel()
    {
        return(mObjModel);
    }

    @PostConstruct
    protected void initPanel()
    {
        add(getFieldsPanel(), BorderLayout.NORTH);
        add(getButtonsPanel(), BorderLayout.SOUTH);
    }

    protected void setButtonActions(final Action[] arrButtonActions)
    {
        mArrButtonActions = arrButtonActions;

        if(getButtonsPanel() != null)
            getButtonsPanel().setButtons(mArrButtonActions);
    }

    protected void setButtonsPanel(final ButtonsPanel pnlButtons)
    {
        mPnlButtons = pnlButtons;

        mPnlButtons.setButtons(getButtonActions());
    }

    protected void setModel(final DetailsPanelModel<ModelType> objModel)
    {
        mObjModel = objModel;
    }
}

更具体,虽然有一个类ScheduleDetailsPanel,它只是DetailsPanel。它不是抽象的,它定义了getFieldPanel()的方法定义。

package net.draconia.test.ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;

import javax.annotation.PostConstruct;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import net.draconia.ApplicationContextProvider;
import net.draconia.test.model.Schedule;

import net.draconia.test.ui.actions.Apply;
import net.draconia.test.ui.actions.Cancel;
import net.draconia.test.ui.actions.Save;
import net.draconia.test.ui.model.DetailsPanelModel;
import net.draconia.test.ui.model.ScheduleDetailsPanelModel;

@Component
public class ScheduleDetailsPanel extends DetailsPanel<Schedule>
{
    private static final long serialVersionUID = -3654518916162120544L;

    @Autowired
    private Apply<Schedule> mActApply;

    @Autowired
    private Cancel<Schedule> mActCancel;

    private JLabel mLblId, mLblName;
    private JTextField mTxtId, mTxtName;

    @Autowired
    private Save<Schedule> mActSave;

    public ScheduleDetailsPanel()
    {
        super(new BorderLayout(5, 5));

        setButtonActions(new Action[] {getApplyAction(), getSaveAction(), getCancelAction()});
    }

    protected Apply<Schedule> getApplyAction()
    {
        return(mActApply);
    }

    protected Cancel<Schedule> getCancelAction()
    {
        return(mActCancel);
    }

    protected JPanel getFieldsPanel()
    {
        EnablablePanel pnlFields = new EnablablePanel(new GridBagLayout());

        return(pnlFields);
    }

    protected JTextField getIdField()
    {
        return(mTxtId);
    }

    protected JLabel getIdLabel()
    {
        return(mLblId);
    }

    protected JTextField getNameField()
    {
        return(mTxtName);
    }

    protected JLabel getNameLabel()
    {
        return(mLblName);
    }

    protected Save<Schedule> getSaveAction()
    {
        return(mActSave);
    }

    @PostConstruct
    protected void initPanel()
    {
        GridBagConstraints objConstraints = new GridBagConstraints();

        objConstraints.gridx = objConstraints.gridy = 0;
        objConstraints.gridwidth = objConstraints.gridheight = 1;
        objConstraints.fill = GridBagConstraints.BOTH;
        objConstraints.anchor = GridBagConstraints.NORTHWEST;
        objConstraints.insets = new Insets(5, 5, 5, 5);
        add(getIdLabel(), objConstraints);

        objConstraints.gridx++;
        add(getIdField(), objConstraints);

        objConstraints.gridx = 0;
        objConstraints.gridy++;
        add(getNameLabel(), objConstraints);

        objConstraints.gridx++;
        add(getNameField(), objConstraints);
    }

    protected void setApplyAction(final Apply<Schedule> actApply)
    {
        mActApply = actApply;
    }

    protected void setCancelAction(final Cancel<Schedule> actCancel)
    {
        mActCancel = actCancel;
    }

    @Autowired
    @Qualifier("selectScheduleDialogDetailsId")
    protected void setIdField(final JTextField txtId)
    {
        mTxtId = txtId;

        mTxtId.setBorder(BorderFactory.createLoweredBevelBorder());
        mTxtId.setEditable(false);
        mTxtId.setFont(getFont());
    }

    @Autowired
    @Qualifier("selectScheduleDialogDetailsIdLabel")
    protected void setIdLabel(final JLabel lblId)
    {
        mLblId = lblId;

        mLblId.setDisplayedMnemonic(KeyEvent.VK_I);
        mLblId.setFont(getFont().deriveFont(Font.BOLD));
        mLblId.setLabelFor(getIdField());
        mLblId.setOpaque(false);
    }

    @Autowired
    @Qualifier("selectScheduleDialogDetailsName")
    protected void setNameField(final JTextField txtName)
    {
        mTxtName = txtName;

        mTxtName.setBorder(BorderFactory.createLoweredBevelBorder());
        mTxtName.setEditable(false);
        mTxtName.setFont(getFont());
    }

    @Autowired
    @Qualifier("selectScheduleDialogDetailsNameLabel")
    protected void setNameLabel(final JLabel lblName)
    {
        mLblName = lblName;

        mLblName.setDisplayedMnemonic(KeyEvent.VK_N);
        mLblName.setFont(getFont().deriveFont(Font.BOLD));
        mLblName.setLabelFor(getNameField());
        mLblName.setOpaque(false);
    }

    protected void setSaveAction(final Save<Schedule> actSave)
    {
        mActSave = actSave;
    }
}

Schedule的结构有点无关紧要但是对于这个测试的情况和现在的每个人都是一般的,它只是一个Id是一个整数和一个String是一个字符串,但它取自我计划的一个更大的项目当我开始工作时重新整合。

发生了什么 - 如果你看到 - 在Detailspanel抽象类中,mObjModel之前有@Autowired,但它最后会变为null。是什么赋予了?是因为DetailsPanelModel是一个抽象类吗?我怀疑的唯一原因是因为我放了一个ApplicationContextAware的ApplicationContextProware类并且自动装配了那个并且ALSO为null所以我不知道发生了什么。我正在尝试获得基类中的通用功能以及子类中的特定功能,因为正确设计了OOP。请协助。

0 个答案:

没有答案