45

i am looking for samples or tutorials of using Spring in a standalone (desktop/swing) application, i searched a lot but couldn't get to something useful, all the examples and tutorials are for web applications, please advise.

fresh_dev
  • 6,694
  • 23
  • 67
  • 95
  • 1
    Maven is a build and dependency resolution system, Hibernate is an OR framework, and Spring is a rather large conglomerate of frameworks. What exactly do you want to achieve? There are tons of tutorials (and even better: official documentation) for Maven, Hibernate and the Spring framework by itself. – joschi Nov 12 '11 at 15:06
  • What's the specific issue? As long as you init the Spring context (pretty easy), it's just another app. – Dave Newton Nov 12 '11 at 16:28
  • 2
    i don't know how to use combination of those technologies in desktop application, i have used them in web applications only. – fresh_dev Nov 12 '11 at 18:48
  • 1
    @fresh_dev Right, but I'm asking what you think the differences are--Maven is totally unrelated to the type of app; it builds. If you're configuring Hibernate via Spring, you just need to init the Spring context, otherwise Hibernate initializes itself from config(s) on the classpath. – Dave Newton Nov 12 '11 at 20:06
  • ok, so any kikstart link for desktop application with maven ? – fresh_dev Nov 12 '11 at 20:29

11 Answers11

33
  1. Create the standalone application with maven, as pointed here:

    Create a standalone application with Maven

  2. Put the applicationContext in classpath, and load it in the main class as follows:

    ClassPathXmlApplicationContext ctx = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
    

    See full article here:

    http://www.devdaily.com/blog/post/java/load-spring-application-context-file-java-swing-application

Community
  • 1
  • 1
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
22

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()
    }
}
Scott
  • 16,711
  • 14
  • 75
  • 120
3

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.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
3

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.

ManuPK
  • 11,623
  • 10
  • 57
  • 76
2

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.

Michael W
  • 3,515
  • 8
  • 39
  • 62
  • 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. – Michael W Mar 21 '16 at 13:46
1

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.

Madhu V Rao
  • 917
  • 1
  • 12
  • 24
1

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.

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
1

This is the first thing I found on google. It looks fair good too.

http://www.mkyong.com/spring/maven-spring-hibernate-annotation-mysql-example/

bnully
  • 31
  • 2
1

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Following 4 libraries are needed for a minimal standalone Spring application :

A good example is given here.

Gonçalo Ribeiro
  • 216
  • 1
  • 12
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

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.

Mark Wood
  • 356
  • 1
  • 9