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
Userclass - Define a
Usersingleton in the Spring configuration class - Use the
Usersingleton in themain()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
- In Eclipse, in the File menu, select New | Project....
- Under Maven, select Maven Project and click on Next >.
- Select the Create a simple project (skip archetype selection) checkbox and click on Next >.
- For the Group Id field, enter
com.springcookbook. For the Artifact Id field, enterspringapp. 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
- Create the
com.springcookbook.configJava package; in the left-hand side pane Package Explorer, right-click on the project and select New | Package…. - In the
com.springcookbook.configpackage, create theAppConfigclass. 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
- Create the
com.springcookbook.mainpackage with theMainclass containing themain()method:package com.springcookbook.main; public class Main { public static void main(String[] args) { } } - 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(); - 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.