java - @Autowired 类中的监听器被调用两次

标签 java spring actionlistener autowired

这是 Autowiring 类:

 @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问题,如有帮助,我们将不胜感激?

最佳答案

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

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

关于java - @Autowired 类中的监听器被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591870/

相关文章:

java - 使用我的类包含的对象实现所有接口(interface)方法

java - 有没有办法让 Maven 同时构建 1.5 和 1.6 字节码 jar?

jsf - action 和 actionListener 的区别

java - 如何使用 Java2D 在 GUI 中使用 ActionListener 调用方法

java - 可以通过指标 :9443 port 访问 8080 端口中的 Spring WebFlux 端点

java - 不同的按钮执行不同的操作

java - 如何优化大文件处理的内存使用

java - 使用 jax-rs 和 cxf 进行客户端身份验证

java - Tomcat:已添加其他 JAR - 上下文重新加载

java - 无法获取 JDBC 连接/事务未激活