Results 1 to 6 of 6

Thread: Loading a spring context in a Swing app

  1. #1

    Default 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. #2
    Join Date
    Aug 2005
    Location
    Lexington, KY
    Posts
    36

    Default

    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.
    Jamie Bisotti

  3. #3

    Default

    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

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5

    Default

    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

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •