popcorn maker kohls Spring Web Application Example | Examples Java Code Geeks – 2018
Monday, January 21, 2019

popcorn maker kohls Spring Web Application Example | Examples Java Code Geeks – 2018

DZone
/ Java Zone

Over a million developers have joined DZone.
Log In / Sign Up


  • node.title

    node.type
    ·
    node.urlSource.name
    ·


    by node.authors[0].realName

    Download

    node.downloads

  • totalResults search results

  • Refcardz
  • Guides
  • Zones
  • Jobs
  • |
  • Agile

  • AI

  • Big Data

  • Cloud

  • Database

  • DevOps

  • Integration

  • IoT

  • Java

  • Microservices

  • Open Source

  • Performance

  • Security

  • Web Dev

announcement.body

announcement.title

Let’s be friends:













Configuring Spring in Stand-Alone Apps

DZone’s Guide to

Configuring Spring in Stand-Alone Apps

Here’s a quick lesson in bringing Spring Configuration classes and functionality to your own stand-alone apps in the event you need them.


by

Tomasz Janczewski

·

Apr. 04, 18
·
Java
Zone

·

Free Resource


Like (17)



Comment (3)

Save


Tweet
formatCount

Views



  • Edit
  • Delete
  • articles[0].isLocked
    ? ‘Enable’ : ‘Disable’ comments
  • articles[0].isLimited ? ‘Remove comment limits’ : ‘Enable moderated comments’

Join the DZone community and get the full member experience.

Join For Free

Atomist automates your software deliver experience . It’s how modern teams deliver modern software.

Spring is a powerful framework — and not only for dependency injection. It can strongly benefit applications as a whole. Sometimes, you need to create your own highly customized stand-alone application instead using an out-of-the-box Spring Boot solution.

Typical cases are custom utility applications used for one-time jobs like email scraping for needed information or custom database migration.

In those cases, you could use Spring Batch, but sometimes, if it is a one-time job, it is easier to just write own code.

You do not need to resign from using Spring in that case — just add it as a stand-alone context and use your favorite Spring features.

Context Loading in Stand-Alone Apps: The Most Critical Feature

AnnotationConfigApplicationContext is the most important class for loading Spring beans in stand-alone applications. It allows annotated classes as inputs, including component package scans and @Configutrationmarked classes. In fact, this is the core of every stand-alone Spring application.

Typically, using AnnotationConfigApplicationContext looks like:

public static void main(String[] args) AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh();

Where AppConfig.class is a separate @Configuration class with all needed beans.

In less complicated cases application main class could be also configuration, then code look like following

@ComponentScan(basePackages = "YOUR_COMPONENTS_PACKAGE")
@Configuration
public class ApplicationX private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationX .class); private SomeService someService; @Autowired public ApplicationX(SomeService someService) this. someService = someService; public static void main(String[] args) ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationX.class); ApplicationX appx = context.getBean(ApplicationX .class); appx.start(); private void start() LOGGER.info("ApplicationX started."); // do stuff 

The constructor of the AnnotationConfigApplicationContext class takes multiple arguments of the @Configuration class. That means all configuration could be passed at once during the creation of the context.

That functionality enables adding separate configurations for separate components like databases or custom security features.

Configuration Classes: What They Are

All classes annotated by @Configuration in Spring are considered as configuration classes, but what does this really mean?

According to the Javadoc, the annotation @Configuraion indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

So it is like a factory class to produce some beans declared in it.

The simplest example of a @Configuration class looks like the following:

@Configuration
public class ConnectionConfig @Bean public MyConnectionService myConnection() // some stuff 

Summary

Adding a Spring context to a stand-alone application is not difficult, and having it can significantly benefit applications. There is nothing wrong with using your own customized applications for one-time jobs, and Spring has special features to help with dependency injection in stand-alone applications.

By the techniques presented in this article, a developer can add their own Spring beans and inject them where needed — or add whole components like Spring Data to a stand-alone application.

Useful resources

  • https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html
  • https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/AnnotationConfigApplicationContext.html

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Like This Article? Read More From DZone


Flavors of Spring Application Context Configuration

What Is a Spring Context?


Programmatic Registration of Java Configuration Beans

Free DZone Refcard

Java Containerization

DOWNLOAD
Topics:
java
,
spring
,
context
,
configuration as code
,
bean
,
standalone code
,
tutorial


Like (17)



Comment (3)

Save


Tweet
articles[0].views

Views



  • Edit
  • Delete
  • articles[0].isLocked
    ? ‘Enable’ : ‘Disable’ comments
  • articles[0].isLimited ? ‘Remove comment limits’ : ‘Enable moderated comments’

Opinions expressed by DZone contributors are their own.


Java Partner Resources

Designing Reactive Systems: The Role Of Actors In Distributed Architecture
Lightbend

Gateway to Data Driven Operation & Digital Transformation
Cdata

Predictive Analytics + Big Data Quality: A Love Story
Melissa

Microservices for Java Developers: A Hands-On Introduction to Frameworks & Containers
Red Hat Developer Program

Use this tool to look at the contents of GitHub and classify code based on the programming language used
IBM

Advanced Linux Commands [Cheat Sheet]
Red Hat Developer Program

Assess Your Open Source Software (OSS) Management with FlexeraOSS.com
Flexera

Distribute microservices between the private and public clusters, yet maintain bi-directional connectivity
IBM

Developing Reactive Microservices: Enterprise Implementation in Java
Lightbend

SQL Abstraction for NoSQL & Big Data
Cdata

Migrating to Microservice Databases
Red Hat Developer Program

Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development
Red Hat Developer Program

Java Partner Resources

Designing Reactive Systems: The Role Of Actors In Distributed Architecture
Lightbend

Gateway to Data Driven Operation & Digital Transformation
Cdata

Predictive Analytics + Big Data Quality: A Love Story
Melissa

Microservices for Java Developers: A Hands-On Introduction to Frameworks & Containers
Red Hat Developer Program

    editionName

  • node.blurb

    Free node.type

    ::node.title




parent.title

parent.tldr

parent.linkDescription

parent.urlSource.name

by {}

· date:’MMM. dd, yyyy’ parent.linkDate
· parent.portal.name Zone


Tweet
parent.views ViewsClicks

  • Edit
  • Delete
  • parent.isLocked ? ‘Enable’ : ‘Disable’ comments
  • parent.isLimited ? ‘Remove comment limits’ : ‘Enable moderated comments’

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .

Using Spring in a standalone application

Ask Question


up vote
43
down vote

favorite

19

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.

java spring

share | improve this question

edited Nov 12 ’11 at 18:59

asked Nov 12 ’11 at 14:58

fresh_dev

2,819205792

  • 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

add a comment  | 

11 Answers
11

active

oldest

votes


up vote
30
down vote

accepted

  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

share | improve this answer

edited May 23 ’17 at 12:17

Community

11

answered Dec 10 ’11 at 18:32

Mahmoud Saleh

15.5k99286446

add a comment  | 


up vote
20
down vote

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() 

share | improve this answer

answered Apr 9 ’13 at 23:07

Scott

12.5k1058112

add a comment  | 


up vote
3
down vote

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.

share | improve this answer

answered Nov 12 ’11 at 16:06

ManuPK

9,36734373

add a comment  | 


up vote
3
down vote

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.

share | improve this answer

answered Jun 17 ’14 at 19:11

Alexander Mills

1

add a comment  | 


up vote
2
down vote

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.

share | improve this answer

edited Mar 21 ’16 at 13:45

answered Dec 10 ’11 at 22:00

Mick

2,29653053

  • 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.
    –  Mick
    Mar 21 ’16 at 13:46


add a comment  | 


up vote
1
down vote

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/

share | improve this answer

answered Nov 12 ’11 at 15:04

bnully

312

add a comment  | 


up vote
1
down vote

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

share | improve this answer

answered Nov 12 ’11 at 15:30

duffymo

268k32313500

add a comment  | 


up vote
1
down vote

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

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.

share | improve this answer

answered Apr 18 ’13 at 4:07

Madhu V Rao

66411121

add a comment  | 


up vote
1
down vote

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.

share | improve this answer

answered Apr 28 ’17 at 16:21

Jan Bodnar

4,24712440

add a comment  | 


up vote
0
down vote

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.

share | improve this answer

answered Mar 19 ’17 at 14:50

Mark Wood

1645

add a comment  | 


up vote
0
down vote

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 .

share | improve this answer

edited Nov 27 at 0:35

Gonçalo Ribeiro

18411

answered Nov 12 ’13 at 12:38

Aniket Thakur

40.7k23183203

  • 6

    good example is a 404
    –  Robert Mark Bram
    Nov 8 ’15 at 1:35

add a comment  | 

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

asked

7 years, 1 month ago

viewed

81,805 times

active

14 days ago

Linked

23

What should the Java main method be for a standalone application (for Spring JMS)?

13

Create a standalone application with Maven

1

How to prevent repeated EntityManager transaction code (open, commit, close) in client app?

0

Couldn’t pass the @bean value to another class through Autowiring

Related

553

What exactly is Spring Framework for?

23

What should the Java main method be for a standalone application (for Spring JMS)?

0

cxf webservice in standalone spring application

1668

What’s the difference between @Component, @Repository & @Service annotations in Spring?

377

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

5

How to configure hibernate in standalone (Swing) application in eclipse?

4

Spring Security in Standalone Application

1

authenticate swing application with spring security

0

Apache Shiro in Spring Boot Desktop Standalone Application

1

How to do unit test for Spring boot standalone application?

Hot Network Questions

  • What would be the necessary conditions to rain bubbles?

  • If I have given my bank details to someone by email what should I do now?

  • How to use "rendere" in a sentence?

  • Are there mistakes on the pre-rolled Basic Set wizard and ranger character sheets?

  • Word for repetition of a same word before and after comma?

  • Company I work at has this "thing" we have to say. What is this "thing" called?

  • How do microstrips actually represent lumped components?

  • Fill the Bucket

  • Why didn’t Loki use the Tesseract?

  • Outsourcing trivial authorless work

  • Given the result of grep -n, how can I open vim in that specific line? (using only keyboard)

  • Through which number we can remove the question mark?

  • Why do governments of developed countries care about mass protests?

  • Are these ‘French children held at gun point’?

  • Unique filtered index and surprising estimation

  • Is it possible to type μ on the bash command line without copying and pasting?

  • Shall structured binding to a copy of a const c-array be const?

  • What type of crank fitting is this?

  • What to teach after scales and chords

  • Show others how I hear myself

  • How do you get temporary hit points?

  • What is the utilitarian purpose of artificial waterfalls?

  • Non scalable unit in tikz

  • Unavailable manager says ‘I’m always available’

more hot questions


question feed

lang-java

Stack Overflow works best with JavaScript enabled