Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SPRING COOKBOOK

You're reading from   SPRING COOKBOOK Over 100 hands-on recipes to build Spring web applications easily and efficiently

Arrow left icon
Product type Paperback
Published in May 2015
Publisher Packt
ISBN-13 9781783985807
Length 234 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Murat Yilmaz Murat Yilmaz
Author Profile Icon Murat Yilmaz
Murat Yilmaz
Jerome Jaglale Jerome Jaglale
Author Profile Icon Jerome Jaglale
Jerome Jaglale
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Creating a Spring Application FREE CHAPTER 2. Defining Beans and Using Dependency Injection 3. Using Controllers and Views 4. Querying a Database 5. Using Forms 6. Managing Security 7. Unit Testing 8. Running Batch Jobs 9. Handling Mobiles and Tablets 10. Connecting to Facebook and Twitter 11. Using the Java RMI, HTTP Invoker, Hessian, and REST 12. Using Aspect-oriented Programming Index

Using Spring in a standard Java application

In this recipe, we will build a standard Java application (not a web application) using Spring. We will:

  • Create a new Maven project
  • Add Spring to it
  • Add a class to configure Spring
  • Add a User class
  • Define a User singleton in the Spring configuration class
  • Use the User singleton in the main() method

How to do it…

In this section, we will cover the steps to use Spring in a standard (not web) Java application.

Creating a new Maven project in Eclipse

  1. In Eclipse, in the File menu, select New | Project....
  2. Under Maven, select Maven Project and click on Next >.
  3. Select the Create a simple project (skip archetype selection) checkbox and click on Next >.
  4. For the Group Id field, enter com.springcookbook. For the Artifact Id field, enter springapp. Click on Finish.

Adding Spring to the project using Maven

Open Maven's pom.xml configuration file at the root of the project. Select the pom.xml tab to edit the XML source code directly. Under the project XML node, define the Java and Spring versions and add the Spring Core dependency:

<properties>
  <java.version>1.8</java.version>
  <spring.version>4.1.5.RELEASE</spring.version>
</properties>

<dependencies>
  <!-- Spring Core -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>
</dependencies>

Creating a configuration class for Spring

  1. Create the com.springcookbook.config Java package; in the left-hand side pane Package Explorer, right-click on the project and select New | Package….
  2. In the com.springcookbook.config package, create the AppConfig class. In the Source menu, select Organize Imports to add the needed import declarations:
    @Configuration
    public class AppConfig {
    }

Creating the User class

Create a User Java class with two String fields:

public class User {
  private String name;
  private String skill;
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSkill() {
    return skill;
  }
  public void setSkill(String skill) {
    this.skill = skill;
  }
}

Defining a User singleton in the Spring configuration class

In the AppConfig class, define a User bean:

  @Bean
  public User admin(){
    User u = new User();
    u.setName("Merlin");
    u.setSkill("Magic");
    return u;
  }

Using the User singleton in the main() method

  1. Create the com.springcookbook.main package with the Main class containing the main() method:
    package com.springcookbook.main;
    public class Main {
      public static void main(String[] args) {
    }
    }
  2. In the main() method, retrieve the User singleton and print its properties:
    AnnotationConfigApplicationContext springContext = new AnnotationConfigApplicationContext(AppConfig.class);
    
    User admin = (User) springContext.getBean("admin");
    
    System.out.println("admin name: " + admin.getName());
    System.out.println("admin skill: " + admin.getSkill());
    
    springContext.close();
  3. Test whether it's working; in the Run menu, select Run.
    Using the User singleton in the main() method

How it works...

We created a Java project to which we added Spring. We defined a User bean called admin (the bean name is by default the bean method name). Spring beans are explained in the next chapter.

In the Main class, we created a Spring context object from the AppConfig class and retrieved the admin bean from it. We used the bean and finally, closed the Spring context.

You have been reading a chapter from
SPRING COOKBOOK
Published in: May 2015
Publisher: Packt
ISBN-13: 9781783985807
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image