Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Using Spring outside a WebApp

  1. #1
    Join Date
    Jun 2007
    Posts
    3

    Default Using Spring outside a WebApp

    Hi,

    Apologies if this is a daft question...

    I have used Spring in the past and found it very useful for injecting my dependencies and otherwise configuring my application. However I have never built a Spring app from scratch - the basic project has always been in place.

    I'd like to use Spring to configure my current application. It isn't currently a web app, however, there may be a presentation web layer added in the future. Any start up guide I have found so far assumes a current web layer.

    So I suppose my first question is: is Spring a suitable tool for the job? And secondly, if it is, the can anyone point me towards a tutorial that describes how to set up a non-web Spring application?

    Again, apologies if this is a silly question and/or I've missed a fundamental point somewhere along the line

    Cheers

  2. #2
    Join Date
    Aug 2006
    Posts
    382

    Default My app is servere-based, not web-based

    I have a server app that provides services without any web components. Its not too hard. You just have to code some startup class.
    Code:
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyServerApp{
    
    	public static void main(String[] args) {
    		ApplicationContext appContext =
    			new ClassPathXmlApplicationContext(
    					new String[] {"myServerApplicationContext.xml"});
    		
    		/* Touching the bean will cause it to get instantiated. */
    		appContext.getBean("serverBean1");
    		appContext.getBean("serverBean2");
    		
    		try {
    			while (true) {
    				Thread.sleep(10000);
    			}
    		} catch (InterruptedException e) {
    			System.out.println("My server app is shutting down.");
    		}
    	}
    }
    This taps the beans, and Spring creates them. My beans expose themselves through CORBA, but you can expose them through any means you wish. Spring is great at supporting this. And if you wish to write something like a Swing front end, just give it something like the code above, and have a separate clientApplicationContext.xml with the necessary hooks to talk by remoting to the server app.
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You might also want to check out "3.5.1.2.2. Shutting down the Spring IoC container gracefully in non-web applications".
    http://www.springframework.org/docs/...disposablebean
    Last edited by karldmoore; Aug 27th, 2007 at 02:07 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #4
    Join Date
    Aug 2006
    Posts
    382

    Default Good idea

    Quote Originally Posted by karldmoore View Post
    You might also want to check out "3.5.1.2.2. Shutting down the Spring IoC container gracefully in non-web applications".
    http://www.springframework.org/docs/...disposablebean

    Thanks Karl!
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  5. #5
    Join Date
    Dec 2011
    Posts
    17

    Default

    Sorry for bringing back to life such an old thread, but I've got a small question. In my app I'm doing pretty much what you suggested:
    Code:
    public class ManagerApp {
    
        public static void main(String[] args) {
            AbstractApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"manager-context.xml", "dao-context.xml"});
    
            CompanyDao dao = (CompanyDao) appContext.getBean("companyDao");
            List<Company> companies = dao.getCompanies();
            System.out.println("company count: " + companies.size());
    
            appContext.registerShutdownHook();
            System.out.println("END");
        }
    }
    but somehow the app keeps running after printing "END".
    From what I see, this is related to the fact that in my application context I've got an org.springframework.scheduling.quartz.SchedulerFac toryBean running some threads periodically...

    How can I shut it all down?

    Thanks and best regards,
    Peter

  6. #6
    Join Date
    Aug 2006
    Posts
    382

    Default

    If you want the app to shut itself down, then nothing is more succinct than "System.exit(0);"
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  7. #7
    Join Date
    Dec 2011
    Posts
    17

    Default

    Hmm... but I would like to shut everything down gracefully. I was expecting that
    Code:
    appContext.registerShutdownHook();
    was equivalent to shutting down the whole application context in a graceful manner, but the quartz threads keep running :/

  8. #8
    Join Date
    Aug 2006
    Posts
    382

    Default

    registerShutdownHook doesn't actually shut down your app. It just configures the IoC container to shutdown properly when the JVM exist.
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  9. #9
    Join Date
    Dec 2011
    Posts
    17

    Default

    Ok, so this means that when I just add "System.exit(0);" at the end of the above "main" method, the whole JVM along with all threads created by Quartz will exit. And all these threads will be shut down gracefully, as will be the entire IoC Spring container. Correct?

  10. #10
    Join Date
    Aug 2006
    Posts
    382

    Default

    Yes. Things will shut down as gracefully as they can.
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

Posting Permissions

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