Announcement Announcement Module
Collapse
No announcement yet.
Loading a spring context in a Swing app Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loading a spring context in a Swing app

    Hi,

    I'm working on a Swing app that uses Spring and I would like to know how I could get a handle to the app context once it has been created. In my main method, I load all of the context paths using ClassPathXmlApplicationContext(). Then I have Spring launch the Swing app by calling the init() method (creates the JFrame, etc.).

    Now I have some Swing actions that need a handle to the spring context in order to retrieve service beans. How am I suppose to retrieve this app context? Is there a nice helper method provided by Spring that does this, or must I create some sort of singleton to retrieve the app context? Thanks in advance!

    -los

  • #2
    Are you using Spring RCP? If not, you should check it out. Also, any class can implement ApplicationContextAware and automatically be injected with the appContext.

    HTHs.

    Comment


    • #3
      Hi,

      Thanks for the reply. I've just tried what you recommended--implementing ApplicationContextAware--but when I launch my application, the applicationContext is null. I'm not sure if I need to do anything besides implementing this interface. I would assume Spring would find any class that implements this interface and automatically inject the context.

      BTW, I've implemented this in one of my Swing actions. I know this may not be the best way of doing things but for now I need to do a POC of saving data into a DB and this particular action does that.

      Also I've taken a look at SpringRCP. Looks very good. I'll probably incorporate it but later down the road. As of now, I need to persist data into a database and that's 1st priority. Besides, incorporating SpringRCP looks to be major since the Swing front-end has already been established and its pretty big.

      Thanks in advance.

      -los

      Comment


      • #4
        Thanks for the reply. I've just tried what you recommended--implementing ApplicationContextAware--but when I launch my application, the applicationContext is null. I'm not sure if I need to do anything besides implementing this interface. I would assume Spring would find any class that implements this interface and automatically inject the context.
        In order for the applicationContext to be injected, the bean which implements the ApplicationContextAware interface has to be declared inside Spring applicationContext. When you'll retrieve the object, it will contain a reference to the enclosing appContext - try turning on logging to see exactly what happens.

        Comment


        • #5
          I think I did what you said: Here's a snippet of my code:

          ######################
          public class SaveAction extends AbstractTableAction implements ApplicationContextAware
          {
          private ApplicationContext applicationContext = null;

          ...
          public void setApplicationContext(ApplicationContext ctx ) {
          this.applicationContext = ctx;
          }
          ....
          }
          #######################


          snippet of my application context file:

          ################################
          <beans>
          ...

          <bean id="SaveAction" class="com.test.swing.actions.SaveAction">
          </bean>

          ...
          </beans>
          ######################


          Is this all I need or am I missing something. Thanks in advance!

          -los

          Comment


          • #6
            No, everything is properly set.
            Make sure you load the appContext and retrieve the SaveAction from it. If for some reason the appContext is still null, I recommend to turn on logging.

            Comment

            Working...
            X