通过Spring注入Singleton

时间:2014-11-24 13:04:34

标签: java spring singleton autowired applicationcontext

我有两个标签,想要在一个标签中设置文字并将其显示在另一个标签上。因此,此文本将显示在另一个类中。

我有以下架构:

我的applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />

    <context:component-scan base-package="test.DELETEME"
        annotation-config="true" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean id="mainGUI" class="test.DELETEME.MainWdw" />
</beans>

我在这里注入了applicationContext

public class Singleton {

    /**
     * Application Context
     */
    private static ApplicationContext ctx;

    public static void main(String[] args) {
        ctx = new ClassPathXmlApplicationContext("applicationContext_Test.xml");
        MainWdw gui = ctx.getBean("mainGUI", MainWdw.class);
        gui.start();
    }   
}

我在这里创建我的标签并设置我的两个TabPanel,ResultsPanMainTabPan

@Component
public class MainWdw extends JFrame {

    /**
     * UUID
     */
    private static final long serialVersionUID = -4931876787108249107L;

    public static JTabbedPane tabbedPane;

    public static ResultsPan resultsTabPanel; 

    @Autowired
    private MainTabPan mainTabPan; 

    private void makeLayout() throws Exception {

        setLayout(new BorderLayout());

        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    /**
     * create Tab Menu
     * @throws Exception 
     */
    public void createTabBar() throws Exception {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        /*
         * Main View 
         */
        tabbedPane.addTab("Main", mainTabPan.createLayout());
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        /*
         * Result table
         */
        tabbedPane.addTab("Results", resultsTabPanel.createLayout());
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


        //Add the tabbed pane to this panel.
        add(tabbedPane);

        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    }

    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    makeLayout();
                } catch (Exception e) {
                    e.printStackTrace();
                }   
            }
        });
    }

    public static JTabbedPane getInstance() {
        if(tabbedPane == null) {
            tabbedPane = new JTabbedPane();
        }
        return tabbedPane;
    }

    @Autowired
    public void setResultsTabPanel (ResultsPan resultsTabPane){
        MainWdw.resultsTabPanel = resultsTabPane;
    }
}

我的第一个面板是ResultsPan,它应显示文字:

@Component
public class ResultsPan extends JPanel{

    /**
     * UUID.
     */
    private static final long serialVersionUID = 5940818789959562707L;

    public String text = "";

    public JScrollPane createLayout() {
        JPanel panel = new JPanel(new MigLayout(""));
        JScrollPane sp;


        JLabel lab = new JLabel(text);
        lab.setFont(new Font("Tahoma", Font.BOLD, 15));

        panel.add(lab, "wrap");

        sp = new JScrollPane(panel);
        sp.repaint();
        sp.validate();

        return sp;
    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * @param text the text to set
     */
    public void setText(String text) {
        this.text = text;
    }
}

我的第二个面板是MainTabPan,应将文字传递给ResultsTab

@Component
public class MainTabPan extends JPanel {

    /**
     * UUID.
     */
    private static final long serialVersionUID = 5940818789959562707L;

    @Autowired
    public ResultsPan resPan;

    private JTextField txt;

    public JScrollPane createLayout() {
        JPanel panel = new JPanel(new MigLayout(""));
        JScrollPane sp;


        JLabel lab = new JLabel("Set text in ResultsTabPanel: ");
        txt = new JTextField();
        JButton btn = new JButton("Set text");

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String text = txt.getText();
                resPan.createLayout();
                resPan.setText(text);
            }
        });

        panel.add(lab, "wrap");
        panel.add(txt, "growx, wrap");
        panel.add(btn, "wrap");

        sp = new JScrollPane(panel);
        sp.repaint();
        sp.validate();

        return sp;
    }

}

我的问题是,没有任何内容传递给第二个标签,也没有任何内容显示出来。

任何建议我做错了什么?

我非常感谢您的回复!

0 个答案:

没有答案