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.
-
1Maven 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
-
2i 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 Answers
Create the standalone application with maven, as pointed here:
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

- 1
- 1

- 33,303
- 119
- 337
- 498
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()
}
}

- 16,711
- 14
- 75
- 120
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.

- 90,741
- 139
- 482
- 817
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.

- 11,623
- 10
- 57
- 76
When I first started to learn spring I followed these tutorials:
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.

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

- 917
- 1
- 12
- 24
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.

- 10,969
- 6
- 68
- 77
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/

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

- 305,152
- 44
- 369
- 561
Following 4 libraries are needed for a minimal standalone Spring application :
commons-logging.jar (see http://commons.apache.org/logging)
org.springframework.core-2.5.6.A.jar (see
http://www.springsource.org/download)org.springframework.beans-2.5.6.A.jar (see
http://www.springsource.org/download)org.springframework.context-2.5.6.A.jar (see
http://www.springsource.org/download)
A good example is given here.

- 216
- 1
- 12

- 66,731
- 38
- 279
- 289
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.

- 356
- 1
- 9