Results 1 to 8 of 8

Thread: Best practice question: bean factory & standalone app

  1. #1

    Default Best practice question: bean factory & standalone app

    Springers,

    I’m developing a spring-based standalone application, and I was wondering how gurus create their bean factory (or app context) and retrieve the singletons.

    Here is what I’m doing:
    I have a BeanManager class that I instanciate in the main function and that is responsible for creating the bean factory (and reading the config files). This class also keeps a reference to the bean factory.
    The BeanManager class has a getBean method that simply redirects to factory.getBean. Most singletons are autowired to other beans; I only use BeanManager.getBean for the beans I use a bit everywhere (like the bean that contains my application parameters).

    Does it sound right to you, or is there a better, more spring-like, way of handling this?

    Thanks,

    Susie.

  2. #2
    Join Date
    Aug 2004
    Location
    Columbus, OH, USA
    Posts
    133

    Default

    It's how I'd do it. Sounds similar to what I do with JUnit tests (an approach copied from Matt Raible's AppFuse). I have an abstract SpringEnabledTestCase that creates a static ApplicationContext and makes it available to my subclassed business service tests. Of course, you could just as easily make it available to non-subclassed objects. You're right to tie the lifecycle of ApplicationContext to your program's main entry (and I assume, exit) point.

    HTH,
    Scott

  3. #3

    Default

    Thanks Scott for your input. So, a Spring application should have one (and only one) old-style singleton, the one that wraps the app context.

    Susie.

  4. #4
    Join Date
    Aug 2004
    Location
    Columbus, OH, USA
    Posts
    133

    Default

    Yes, just one singleton/factory/locator (what's the politically correct name for it this week?) is all you'll need. BTW I recommend eagerly instantiating the ApplicationContext (versus lazily doing so and flirting with double-checked locking problems or incurring a performance penalty through an instantiation check). Just make your ApplicationContext a static property and instantiate it as classloading time.

    I.e.

    Code:
        private static ApplicationContext ctx = null;
    
        static {
            ctx = new ClassPathXmlApplicationContext(Constants.SPRING_APPLICATION_CONTEXT_CONFIG_FILE);
        }
    Scott

  5. #5
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    There is also something in the factory stuff that allows the container to load in one's own singleton bean on startup?


    http://www.springframework.org/docs/...Bootstrap.html

    quote:
    Code:
    One singleton to rule them all. Reads System properties, which must contain the definition of a bootstrap bean factory using the Properties syntax supported by PropertiesBeanDefinitionReader. The name of the bootstrap factory must be "bootstrapBeanFactory". Thus a typical definition might be: bootstrapBeanFactory.class=com.mycompany.MyBeanFactory 
    
    Use as follows: BeanFactory bf = BeanFactoryBootstrap.getInstance().getBeanFactory();
    Wonder what the use case is for using this rather then the other factories.

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    If you actually have a use-case for BeanFactoryBootstrap, then you are probably better off using one of the singlton variants of BeanFactoryLocator, such as SingletonBeanFactoryLocator...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  7. #7
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Susie,

    You might want to take a look at how we bootstrap Spring in a standalone environment for a Swing-based rich client application, illustrated by the Spring Rich Client Project Petclinic Sample Application.

    Basically, we have a framework class -- org.springframework.richclient.application.Applica tionLauncher -- responsible for loading the main context for the application; invokable via a simple bootstrap class with a main method. For convenience, we have a "ApplicationServices" service locator object with a static accesor for locating common singleton application services (like those for icon and image loading), typically looking up using the Spring application context (but we obviously favor dependency injection/wiring for most things, as you mentioned.)

    Just curious: when you say standalone - what does the app's interface? command line driven? Swing app? other?

    Keith
    Keith Donald
    Core Spring Development Team

  8. #8

    Default

    Keith,


    Quote Originally Posted by kdonald
    You might want to take a look at how we bootstrap Spring in a standalone environment for a Swing-based rich client application
    Thanks, I'll have a look. Digging into the RCP was on my todo list.

    Quote Originally Posted by kdonald
    Just curious: when you say standalone - what does the app's interface? command line driven? Swing app? other?
    It's a swing app.

    Susie

Similar Threads

  1. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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