banner



Can I Use Spring On Standalone Java App

11 Answers 11

Here's a simple example with 2 classes. Wrote in groovy for ease of reading, but will run for you in java too with proper syntax tweaks

Here's your main:

              class Main {      static void main(String[] args) {         def ctx = new AnnotationConfigApplicationContext()         ctx.register(AppConfig.class)         ctx.refresh()          def runner = ctx.getBean("mainRunner")         runner.run()     }      void run() {         println "running from bean"     } }                          

Here's your config bean:

              @Configuration class AppConfig {      @Bean     Main mainRunner() {         new Main()     } }                          

answered Apr 9 '13 at 23:07

AppFuse provides different demo applications, all the source code can be downloaded using maven. You can get the complete code of this demo application which is build using Spring MVC,Spring, Hibernate.

Yes this is a web application, you can dig into it and convert it to a stand alone one.

answered Nov 12 '11 at 16:06

create a Maven project

it will create an Application class for your project

              @Configuration @ComponentScan @EnableAutoConfiguration public class Application {      public static void main(String[] args) {         //SpringApplication.run(YourClass.class, args);           YourClass.main(args);     } }                          

put YourClass main method in there instead of SpringApplication.run(YourClass.class,args);

it works that way just fine.

answered Jun 17 '14 at 19:11

When I first started to learn spring I followed these tutorials:

tutorialspoint

They are fairly basic but will get you up and running quickly. After this is ultimately comes down to what you are going to use it for. Are you looking for IOC, JMS, JDBC/Hibernate support etc etc?

As mentioned already:

                ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext");                              

will bring all your spring beans into your app regardless of what type it is.

answered Dec 10 '11 at 22:00

1

  • Fixed. Unfortunately the 'vanilla' site is gone. This was moved into dzone but they seemed to have disappeared from there also. I've added an an alternative.

    Mar 21 '16 at 13:46

answered Nov 12 '11 at 15:04

Take a look at "Barebones Spring". I think it's a nice, up to date example of how to use Spring 3.

answered Nov 12 '11 at 15:30

This is the tutorial of Spring which I found to be very useful. This explains Spring based on a Standalone application.

https://www.youtube.com/watch?v=GB8k2-Egfv0

Author of this videos also has updated the Maven and Struts videos and explained it in a simple but in an effective way.

I hope it helps.

answered Apr 18 '13 at 4:07

0

I have managed to run a standalone Spring Boot application with Swing.

              public static void main(String[] args) {      ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SwingApp.class)             .headless(false).run(args);      EventQueue.invokeLater(() -> {         SwingApp ex = ctx.getBean(SwingApp.class);         ex.setVisible(true);     }); }                          

We need to use the SpringApplicationBuilder and turn off the headless mode.

              @SpringBootApplication public class SwingApp extends JFrame {                          

The SwingApp is decorated with @SpringBootApplication annotation.

See my Spring Boot Swing integration tutorial for a full working example.

answered Apr 28 '17 at 16:21

So, to boil it down: what makes your application (any type) a Spring application is the presence and use of at least one BeanFactory, usually extended as an ApplicationContext. In a web application you'd likely declare in web.xml a servlet such as DispatcherServlet which takes care of instantiating and initializing the context; in a standalone application your own code just makes and initializes a context, as shown above. The web framework stuff that magically gives you a context is doing pretty much the same thing under the covers.

answered Mar 19 '17 at 14:50

answered Nov 12 '13 at 12:38

0

Not the answer you're looking for? Browse other questions tagged java spring or ask your own question.

Can I Use Spring On Standalone Java App

Source: https://stackoverflow.com/questions/8105217/using-spring-in-a-standalone-application#:~:text=Yes%20this%20is%20a%20web,to%20a%20stand%20alone%20one.&text=put%20YourClass%20main%20method%20in,class%2Cargs)%3B

Posted by: willinghammandked81.blogspot.com

0 Response to "Can I Use Spring On Standalone Java App"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel