mercredi 25 mars 2015

Spring Autowired NullPointerException

I am using spring for dependency injection, so I have a class which uses swing for user interface, however when I want to use @Autowired for any swing objects I get NullPointerException.


Here is the class:



@Component
public class PanelImpl implements Panel {

@Autowired
@Qualifier("getJPanel")
private JPanel panel;

//@Autowired
private JButton getInfoBtn = new JButton();

public PanelImpl() {
init();
}

private void init() {
setPanel();
}

private void setPanel() {
getInfoBtn.setText("Get Info");
panel.add(getInfoBtn);
}

@Override
public JPanel getPanel() {
return panel;
}
}


And here is the Configuration class:



@Configuration
@ComponentScan("ooc")
@PropertySource("app.properties")
public class SpringConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

@Bean
public Panel getPanel() {
Panel PanelImpl = new PanelImpl();
return PanelImpl;
}


@Bean
public JPanel getJPanel(){
return new JPanel();
}

}


So any suggestions how I can use spring for injecting swing objects? Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire