Announcement Announcement Module
No announcement yet.
Using Spring outside a WebApp Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Spring outside a WebApp


    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


  • #2
    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.
    import org.springframework.context.ApplicationContext;
    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. */
    		try {
    			while (true) {
    		} 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.


    • #3
      You might also want to check out " Shutting down the Spring IoC container gracefully in non-web applications".
      Last edited by karldmoore; Aug 27th, 2007, 02:07 PM.


      • #4
        Good idea

        Originally posted by karldmoore View Post
        You might also want to check out " Shutting down the Spring IoC container gracefully in non-web applications".

        Thanks Karl!


        • #5
          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:
          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());
          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,


          • #6
            If you want the app to shut itself down, then nothing is more succinct than "System.exit(0);"


            • #7
              Hmm... but I would like to shut everything down gracefully. I was expecting that
              was equivalent to shutting down the whole application context in a graceful manner, but the quartz threads keep running :/


              • #8
                registerShutdownHook doesn't actually shut down your app. It just configures the IoC container to shutdown properly when the JVM exist.


                • #9
                  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
                    Yes. Things will shut down as gracefully as they can.


                    • #11
                      Exactly what I needed. Thanks a lot