Reusing a Spring Configuration File in the NetBeans Platform

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=""
    <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.


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

Posted by Edvin Syse on December 24, 2011 at 12:21 AM PST #

Hi Geertjan,
Is it possible to separate Spring library to difference module?

Posted by guest on December 24, 2011 at 03:53 PM PST #

Yes, of course it is possible to do that.

Posted by guest on December 24, 2011 at 06:06 PM PST #

Thank for reply.
I known that (read from 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.

Posted by guest on December 24, 2011 at 08:51 PM PST #

No hacks are needed.

Posted by Geertjan on December 24, 2011 at 09:32 PM PST #

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?

Posted by Ishtek on December 25, 2011 at 01:27 AM PST #

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 for me to see your email and send you there the demo I made.


Posted by guest on December 26, 2011 at 12:24 AM PST #

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 [] not found; nested exception is java.lang.ClassNotFoundException: Will not load class org.springframework.context.config.ContextNamespaceHandler arbitrarily from one of ModuleCL@0[] and ModuleCL@1[] starting from SystemClassLoader[56 modules]; see
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions( org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions( org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(

Posted by Ioannis on November 12, 2012 at 09:02 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« June 2016