Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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.

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.