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

  • 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
    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.

    Comment


    • #3
      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, 03:07 PM.

      Comment


      • #4
        Good idea

        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!

        Comment


        • #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:
          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

          Comment


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

            Comment


            • #7
              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 :/

              Comment


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

                Comment


                • #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?

                  Comment


                  • #10
                    Yes. Things will shut down as gracefully as they can.

                    Comment


                    • #11
                      Exactly what I needed. Thanks a lot

                      Comment

                      Working...
                      X