@Autowired类中的监听器调用了两次

时间:2015-07-23 15:31:32

标签: java spring actionlistener autowired

这是 autowired class

 @Component
public class AlfrescoLoginFrame extends javax.swing.JFrame {
    private static final long serialVersionUID = 6302651813469103752L;

    @Autowired
    private MainController controller;

    public AlfrescoLoginFrame() {
        initComponents();
        initActions();

    }


    private void initComponents() {
         try {
            UIManager.setLookAndFeel("com.jgoodies.looks.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel ( "com.alee.laf.WebLookAndFeel" );
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        lbl_user_name = new javax.swing.JLabel();
        lbl_password = new javax.swing.JLabel();
        txt_user_name = new javax.swing.JTextField();
        txt_password = new javax.swing.JTextField();
        bt_connexion = new javax.swing.JButton();
        bt_annuler = new javax.swing.JButton();
        lbl_alfreco_logo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Se connecter");
        lbl_user_name.setText("Nom d'utilisateur");
        lbl_password.setText("Mot de passe");

        bt_connexion.setIcon(new ImageIcon(LOGIN_ICON)); 
        bt_connexion.setText("Connexion");      

        bt_annuler.setIcon(new ImageIcon(LOGOUT_ICON)); 
        bt_annuler.setText("Annuler");


        initActions();

        lbl_alfreco_logo.setIcon(new ImageIcon(ALFRESCO_LOGO));
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(bt_connexion)
                        .addGap(18, 18, 18)
                        .addComponent(bt_annuler, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(lbl_alfreco_logo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_password, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbl_user_name))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_user_name, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(91, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(lbl_alfreco_logo, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_user_name)
                    .addComponent(txt_user_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_password)
                    .addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_connexion)
                    .addComponent(bt_annuler))
                .addGap(25, 25, 25))
        );

        pack();

    }                       



    public void initActions(){
        bt_connexion.addActionListener(new LoginActionListener());
    }

    private class LoginActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //System.out.println("***"+e.getActionCommand());
            // it displays the same event 

            controller.checkLoginCreditsRedirect(txt_user_name.getText(),txt_password.getText());

        }

    }



    // Declaration des variables                   
    private javax.swing.JButton bt_annuler;
    private javax.swing.JButton bt_connexion;
    private javax.swing.JLabel lbl_alfreco_logo;
    private javax.swing.JLabel lbl_password;
    private javax.swing.JLabel lbl_user_name;
    private javax.swing.JTextField txt_password;
    private javax.swing.JTextField txt_user_name;
    public static final String LOGIN_ICON= System.getProperty("user.dir") + "/resource/com/talan/launcher/ui/icon/login_icon.png";
    public static final String ALFRESCO_LOGO = System.getProperty("user.dir") + "/resource/com/talan/launcher/ui/icon/alfresco_logo.jpg";
    public static final String LOGOUT_ICON = System.getProperty("user.dir") + "/resource/com/talan/launcher/ui/icon/logout_icon.png"; }

你可以看到监听器

 public void initActions(){
    bt_connexion.addActionListener(new LoginActionListener());
}

private class LoginActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        //System.out.println("***"+e.getActionCommand());
        // it displays the same event 

        controller.checkLoginCreditsRedirect(txt_user_name.getText(),txt_password.getText());

    }

}

这是 AlfrescoLoginFrame 中的类 MainController 是@Autowired:

  @Component
    public class MainController {
    @Autowired
     private ISessionRepo sessionRepo;
    @Autowired
    private WrongAlfrescoCreditsDialog wrongAlfrescoCredistDiag;



    public MainController() {
        }

    public void checkLoginCreditsRedirect(String login, String password){

           checkLoginCredits(login, password);
           if(sessionRepo.isValid()){
               //This sop executes twice
               System.out.println("Logged in !");

           }else{
             //Also sop executes twice
               System.out.println("Not logged in : wrond credit !");
               wrongAlfrescoCredistDiag.riseWrongCreditsPanel(alfresco_loginFrame);
           }
       }
}

所以 checkLoginCreditsRedirect(String login,String password)方法被调用两次。 我认为这是一个 SPRING 问题,任何帮助都会受到赞赏吗?

2 个答案:

答案 0 :(得分:1)

您拨打initActions();两次。一旦进入构造函数,第二次进入initComponents方法。

答案 1 :(得分:1)

你自己两次调用initActions()。 从构造函数开始,一次initComponents方法

下次遇到这样的问题时:通过添加尝试调试 意外被调用两次的方法中的new Exception().printStackTrace();