@Autowired class 中的侦听器调用了两次

Listener within @Autowired class called twice

这是 自动装配的 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"; }

你可以看到 listener :

 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());

    }

}

这是 class MainController class AlfrescoLoginFrame 中的 @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 问题,任何帮助将不胜感激?

您调用 initActions(); 两次。一次在构造函数中,第二次在 initComponents 方法中。

您自己调用了两次 initActions()。 一次来自您的构造函数,一次来自 initComponents 方法

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