Geertjan's Blog

  • December 24, 2011

Reusing a Spring Configuration File in the NetBeans Platform

Geertjan Wielenga
Product Manager

Here's a small demo showing how to access a Spring configuration file in a NetBeans Platform application and use one of its beans:


Run the example and you'll see this:

Open the sources in NetBeans IDE and you'll see this:

The key to understanding how it all works is to look in the SpringTopComponent shown in the sources above. The constructor is as follows:

public SpringTopComponent() {
setLayout(new BorderLayout());
String[] contextPaths = new String[]{"org/spring/sample/app-context.xml"};
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(contextPaths);
add(ctx.getBean("mainPanel", JPanel.class), BorderLayout.CENTER);

So, there you have a reference to a Spring configuration file, which in this case has this content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="mainPanel" class="org.spring.sample.MyJPanel" init-method="init">
        <property name="panelComponents">
                <ref bean="textField1"/>
                <ref bean="textField2"/>
                <ref bean="textField3"/>
                <ref bean="buttonPanel"/>
    <bean id="buttonPanel" class="org.spring.sample.MyJPanel" init-method="init">
        <property name="panelComponents">
                <ref bean="button1"/>
    <bean id="textField1" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 1</value>
    <bean id="textField2" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 2</value>
    <bean id="textField3" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 3</value>
    <bean id="button1" class="org.spring.sample.MyJButton" init-method="init">
        <property name="actionListener">
            <ref bean="myButtonActionListener"/>
        <property name="text">
            <value>Click me!</value>
    <bean id="myButtonActionListener" class="org.spring.sample.MyActionListener"/>

The references to MyJPanel, MyJTextField, MyJButton, and MyActionListener are Java classes that you'll find in the sources. I.e., here you have a GUI component in the NetBeans Platform that is built up from components registered in a Spring configuration file. Here, for example, is the JPanel referred to above:

public class MyJPanel extends JPanel {
    private List<Component> panelComponents;
    public void setPanelComponents(List<Component> panelComponents) {
        this.panelComponents = panelComponents;
    public void init() {
        setLayout(new MigLayout());
        for (Component component : panelComponents) {

The fact that you can call ClassPathXmlApplicationContext.getBean means that you can call that for any other Spring bean too and integrate calls to your Spring configuration file in your NetBeans Platform application in the same way as shown above.

Join the discussion

Comments ( 8 )
  • Edvin Syse Saturday, December 24, 2011

    ClassPathXmlApplicationContext has a constructor that takes a String, so you don't need to wrap the context path in a String array :)

  • guest Saturday, December 24, 2011

    Hi Geertjan,

    Is it possible to separate Spring library to difference module?

  • guest Sunday, December 25, 2011

    Yes, of course it is possible to do that.

  • guest Sunday, December 25, 2011

    Thank for reply.

    I known that (read from http://wiki.netbeans.org/NetBeansPlatformWithSpring) in previous Netbeans version it need to hack ClassLoader to access an implementation code to let Spring access those class. Is 7.1 can do allow Spring access implementation class without any hack ClassLoader? If it can do, please blog to your follower.


  • Geertjan Sunday, December 25, 2011

    No hacks are needed.

  • Ishtek Sunday, December 25, 2011

    Is it possible to have runtime screen layout using Swing API based on some pre-defined screen meta data those are being passed at runtime only?

  • guest Monday, December 26, 2011

    Hi Geertjan:

    Sorry for the delay, I've made an example demo based on the plugin that I'm developing, but for some reason I don't know why I can't see the email address you wrote me in the Netbeans forums (it appears to me as "address-removed"), please, if you can send me an email to this address jeortiz@hlg.desoft.cu for me to see your email and send you there the demo I made.


  • Ioannis Monday, November 12, 2012

    This won't work if you add another module based on spring and you load the context, you get

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [META-INF/spring-config.xml]; nested exception is org.springframework.beans.FatalBeanException: NamespaceHandler class [org.springframework.context.config.ContextNamespaceHandler] for namespace [http://www.springframework.org/schema/context] not found; nested exception is java.lang.ClassNotFoundException: Will not load class org.springframework.context.config.ContextNamespaceHandler arbitrarily from one of ModuleCL@0[com.xxx.service.UserAuthentication] and ModuleCL@1[com.xxx.BusinessService] starting from SystemClassLoader[56 modules]; see http://wiki.netbeans.org/DevFaqModuleCCE

    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.