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:

http://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.1/misc/SpringDemo

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() {

    initComponents();

    setName(Bundle.CTL_SpringTopComponent());
    setToolTipText(Bundle.HINT_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"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="mainPanel" class="org.spring.sample.MyJPanel" init-method="init">
        <property name="panelComponents">
            <list>
                <ref bean="textField1"/>
                <ref bean="textField2"/>
                <ref bean="textField3"/>
                <ref bean="buttonPanel"/>
            </list>
        </property>
    </bean>

    <bean id="buttonPanel" class="org.spring.sample.MyJPanel" init-method="init">
        <property name="panelComponents">
            <list>
                <ref bean="button1"/>
            </list>
        </property>
    </bean>

    <bean id="textField1" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 1</value>
        </property>
    </bean>

    <bean id="textField2" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 2</value>
        </property>
    </bean>

    <bean id="textField3" class="org.spring.sample.MyJTextField" init-method="init">
        <property name="text">
            <value>text field 3</value>
        </property>
    </bean>

    <bean id="button1" class="org.spring.sample.MyJButton" init-method="init">
        <property name="actionListener">
            <ref bean="myButtonActionListener"/>
        </property>
        <property name="text">
            <value>Click me!</value>
        </property>
    </bean>

    <bean id="myButtonActionListener" class="org.spring.sample.MyActionListener"/>

</beans> 
 
  

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) {
            add(component);
        }
    }
 
}

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.

Comments:

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 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.
Thank

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

Carmen

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 [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)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)

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

Post a Comment:
  • HTML Syntax: NOT allowed
About

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.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today