This blog post is about the a step by step SpringBoot Gradle IntelliJ Example. We will go through following table of content. SpringBoot has made the application development simpler by using the annotations. In old days, when writing the java MVC applications, we had to deal with various components Beans, jsp, POJO etc. Spring has made the app. development smooth by using annotations.
we are going to see the SpringBoot example with Gradle Build Tool and IntelliJ IDE.
Page Contents
Spring Containers
Spring containers are like below one. Which holds the various type of materials within it.
The similar concept is at the core of spring framework. i.e. spring container. Which hold the various types of beans or objects within it. And when
Various spring Annotations Used
@Component - Spring will search for the Bean with @Component annotation and will autowire it, wherever a reference for it is created in application. @Scope - Scope annotation defines the visibility of the bean to various spring contexts. with @Scope(value = "singleton") or @Scope(value = "prototype"), spring creates singleton or prototype bean objects. @SpringBootApplication - It defines the entry point of the application. Same as a main() method in plain java application. For further details about various spring and springboot annotations, refer this.
- Setup the SpringBoot example with Gradle as a Build tool.
To setup the spring boot application we can use either the Spring Initializr to generate the SpringBoot project and use it after unzipping it or we can install the Spring Assistant IntelliJ plugin.
or in your intelliJ go to Preferences -> Plugins -> search for “Spring Assistant”.
And then create a new project by File -> New -> project -> Spring Assistant -> set the groupid, artifactid -> next and Finish.
Write code
Employee.java
package com.atozlearner.basicspringbootapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(value = "singleton") // with @Scope , it creates singleton or prototype objects
//@Scope(value = "prototype")
public class Employee {
@Autowired // search the object with type in spring container
@Qualifier(value = "sal") // search the object with name in spring container
private Salary salary;
public Employee(){ System.out.println("Employee object is created...");}
private String empName;
private int empId;
public Salary getSalary() { return salary; }
public void setSalary(Salary salary) { this.salary = salary; }
public String getEmpName() { return empName; }
public void setEmpName(String empName) { this.empName = empName; }
public int getEmpId() { return empId; }
public void setEmpId(int empId) { this.empId = empId; }
@Override
public String toString() {
return "Employee {" +
"salary=" + salary +
", empName='" + empName + '\'' +
", empId=" + empId +
'}';
}
}
Salary.java
package com.atozlearner.basicspringbootapp;
import org.springframework.stereotype.Component;
@Component("sal") // with @Component annotation, it will create an object in spring container
public class Salary {
private Integer empid;
private Integer salary;
public Integer getEmpid() {
return empid;
}
public void setEmpid(Integer empid) {
this.empid = empid;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Salary{" +
"empid='" + empid + '\'' +
", salary='" + salary + '\'' +
'}';
}
}
BasicspringbootappApplication.java
package com.atozlearner.basicspringbootapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class BasicspringbootappApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
Salary salary = context.getBean(Salary.class);
salary.setEmpid(100);
salary.setSalary(10000);
Employee employee = context.getBean(Employee.class);
employee.setEmpId(100);
employee.setEmpName("User1");
employee.setSalary(salary);
System.out.println(employee);
}
}
Run the application.
Run the application via right click on BasicspringbootappApplication.java class
./gradlew bootRun –> on mac/linux
gradlew bootRun –> on windows
Output:
- Conclusion – We are able to setup and run a sample SpringBoot example with Gradle within IntelliJ IDE. If you liked the post, please like and comment.