I am facing the problem of needing a certain instance of a class which is Autowired. I have the following Architecture:
My application context:
<?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>
Here I inject the application context:
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();
}
}
Furthermore, I am wirering my GUI together.
@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;
}
}
My class where I need the instance from:
@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;
}
}
However, I get an error:
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)
Any recommendations how I can get a ResultsTabPanel
instance or how I could set the ResultsTabPanel
globally?
I appreciate your answers!
As said by the error message:
Autowired annotation is not supported on static fields
So you can't autowire a static dependency. Thus, you need to remove the static or to create a non-static setter on wich you will autowire the resultsTabPane from Spring and which will set the static variable:
@Autowired
public void setResultsTabPanel (ResultsTabPanel resultsTabPane){
MainWindow.resultsTabPane = resultsTabPane;
}