Kick-starting Spring Boot

An introduction to programming with Spring Boot
Earlier, we looked at Spring, learnt about the dependency injections and how the XML configuration works.

With the next step of Spring, we will learn about Spring Boot and the annotation configuration when programming with Spring Boot.

Spring Boot

Features of Spring Boot

  • Provides dependency and configuration.
  • Allows the programmer to do convention over configuration, meaning it allows the programmer to focus more on the actual functions of the program rather than its configurations.
  • Produces production-ready programs.
  • Provides an embedded server.
Other than the mentioned features, Spring Boot has all the features of a Spring application discussed before.

Creating a Spring Boot Application

Setting Up a Spring Boot Application on IntelliJ Idea

1. Load IntelliJ Idea IDE

2. Go to File -> New -> Project

3. From the side panel, select Spring Initializer.

4. Select the JDK to be used and Default service URL.


5. Click Next.

6. Enter appropriate names for the group and artifact IDs.


7. Click Next.

8. From the Dependencies dialog box, you can select the relevant Spring Boot version that is required.


9. It is also possible to select the relevant dependencies that are required for the project from the same dialog box. For the purpose of this basic tutorial, we will not select any dependencies.


10. Enter a name for the project and click Finish.


Congratulations! You have now set up a simple Spring Boot application!

Now, let us look at a simple program with annotation configuration.

Programming with Spring Boot

1. Create a new class inside the Java folder (Inside the com.example folder) named Student.
The class should contain the following code;

package com.bootone.demo;

import org.springframework.stereotype.Component;

@Component
public class Student {
public int studentId;
public String studentName;

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public void show(){
System.out.println("Showing student.");
}
}
The @Component annotation lets the Spring framework know that Student is a Bean that should be contained within the Spring Container for when required.

2. Inside the demo application, modify the code as follows;

package com.bootone.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run                                                 (DemoApplication.class, args);

Student student1 = context.getBean(Student.class);

student1.show();
}
}

As you can see, there is loose couple attained by the fact that the program is not highly dependent on the Student class.

If you run this application, it will now give the following output;


Let us modify this code to include another class.

3. Create a new class in the same folder named Subject and program it as follows;

package com.bootone.demo;

import org.springframework.stereotype.Component;

@Component("subject1")
public class Subject {
private int subjectId;
private String subjectName;

public int getSubjectId() {
return subjectId;
}

public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}

public String getSubjectName() {
return subjectName;
}

public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}

public void showSubject(){
System.out.println("Showing subject.");
}
}

4. Modify the Student class in the following manner;

package com.bootone.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Student {
public int studentId;
public String studentName;
@Autowired
@Qualifier("subject1")
public Subject subject;

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public void show(){
System.out.println("Showing student.");
subject.showSubject();
}

public Subject getSubject() {
return subject;
}

public void setSubject(Subject subject) {
this.subject = subject;
}
}
5. No changes are required to be made in the main method.

If you run this application now, it will give the following output;


This was achieved without creating a single object of the Subject class in the main method.

With the help of the @Component annotation in the Subject and by providing a name for it, it was possible to link the two classes without making any changes to the main method.

The @Component annotation made another bean inside the Spring container for the Subject class and called it with the help of the @Autowired and @Qualifier annotations.

This makes the programming with dependencies so much easier.

Happy Booting!










Comments

Popular Posts