2014-11-24 1 views
2

Autowired 인 클래스의 특정 인스턴스가 필요하다는 문제에 직면하고 있습니다.Autowired 필드가있는 Spring의 Singelton

내 응용 프로그램 컨텍스트 : 여기

<?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.MainWindow" /> 

</beans> 

내가 응용 프로그램 컨텍스트를 주입 :

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Singleton { 

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

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

는 또한, 내가 함께 내 GUI를 wirering하고 나는 다음과 같은 구조를 가지고있다.

@Component 
public class MainWindow extends JFrame { 


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

    public static JTabbedPane tabbedPane; 

    @Autowired 
    private static ResultsTabPanel resultsTabPanel; 

    /** 
    * create the Layout 
    * @throws Exception 
    */ 
    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", new JPanel()); 
     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; 
    } 
} 
내가에서 인스턴스 필요가 내 클래스

그러나

@Component 
public class ResultsTabPanel extends JPanel{ 

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

    private static ResultsTabPanel instance; 

    /** 
    * Creates the Layout of the REA Tab. 
    * @return 
    */ 
    public JScrollPane createLayout() { 
     JPanel panel = new JPanel(new MigLayout("")); 
     JScrollPane sp; 

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

     panel.add(lab, "wrap"); 

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

     return sp; 
    } 

    public static ResultsTabPanel getInstance() { 
     if(instance == null) { 
      instance = new ResultsTabPanel(); 
     } 
     return instance; 
    } 
} 

, 나는 오류가 발생 :

나는 ResultsTabPanel 인스턴스를 얻을 수있는 방법
1012 [main] WARN org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - Autowired annotation is not supported on static fields: private static test.DELETEME.ResultsTabPanel test.DELETEME.MainWindow.resultsTabPanel 
java.lang.NullPointerException 
    at test.DELETEME.MainWindow.createTabBar(MainWindow.java:60) 
    at test.DELETEME.MainWindow.makeLayout(MainWindow.java:36) 
    at test.DELETEME.MainWindow.access$0(MainWindow.java:31) 
    at test.DELETEME.MainWindow$1.run(MainWindow.java:79) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$400(Unknown Source) 
    at java.awt.EventQueue$2.run(Unknown Source) 
    at java.awt.EventQueue$2.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
com.passlogix.vgo.ho.WindowHandleException: null: test.DELETEME.MainWindow[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] 
    at com.passlogix.vgo.ho.ApplicationWindow.nativeGetWindowHandle(Native Method) 
    at com.passlogix.vgo.ho.ApplicationWindowAccessJava1dot4.getHWnd(ApplicationWindowAccessJava1dot4.java:50) 
    at com.passlogix.vgo.ho.WindowScanner.run(WindowScanner.java:569)  

모든 권고 또는 어떻게 내가 할 수있는 ResultsTabPanel을 전체적으로 설정 하시겠습니까?

답변 해 주셔서 감사합니다.

+1

자세한 내용은 다음 페이지를 참조하십시오. http://stackoverflow.com/questions/10938529/why-cant-we-autowire-static-fields-in-spring –

+1

http://stackoverflow.com/questions/11324372/how-to-make-spring-inject- 정적 필드에 대한 가치 – pomkine

답변

2

으로 오류 메시지가 말했다 :

Autowired annotation is not supported on static fields

그래서 정적 종속 관계를 autowire하기 수 없습니다. 따라서 정적 변수를 제거하거나 스프링에서 resultsTabPane을 autowire하고 정적 변수를 설정할 정적이 아닌 setter를 만들어야합니다.

@Autowired 
public void setResultsTabPanel (ResultsTabPanel resultsTabPane){ 
MainWindow.resultsTabPane = resultsTabPane; 
} 
+0

답변에 대한 Thx! '정적을 제거해야합니까? '라는 것은 무엇을 의미합니까? 어떤 반에서? – mrquad

+0

또한, 어떻게 ResultsTabPanel의 인스턴스를 얻을 수 있습니까? 당신의 대답에 감사드립니다! – mrquad

+1

'MainWindow' 클래스에서 ResultsTabPanel을 비 정적으로 설정하거나 필자가 설명한 설정기를 제공해야합니다. – chouk