๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring First Program using Annotation Based Configuration


Steps to Create Spring First Program

  1. Open any IDE & Create New Project
  2. Add Spring Jar Files (or Dependencies if using MAVEN)
  3. Create POJO Class i.e. Student and Annotate the POJO with "@Component" Annotation
  4. Enable Component Scanning with @ComponentScan
  5. Create Main Class, Start the Container & Access the Beans
1. Open any IDE & Create New Project
  • Open any IDE like Eclipse, IntelliJ, Spring Tool Suit (STS) etc and create new project as shown below.

  • Note : In our case we are using Eclipse IDE.







2. Add Spring Jar Files (or Dependencies if using MAVEN)
  • Add Spring Jar Files (If you are creating simple Java Project) OR Add Spring Dependency (If you are creating MAVEN Project)

  • Note : Here we are taken simple Java Project, MAVEN will be explained in further tutorials.

  • Required Jar Files are as below
    1. spring-core-x.x.x.jar
    2. spring-beans-x.x.x.jar
    3. spring-context-x.x.x.jar
    4. spring-expression-x.x.x.jar
    5. spring-aop-x.x.x.jar
    6. commons-logging-x.x.jar
    Where xxx is the version of jar file







3. Create POJO Class i.e. Student and Annotate the POJO with "@Component" Annotation
  • Create one POJO class i.e. "Student" in package "in.sp.beans" and create one display() method to print Student details.





  • Annotate the Student class with @Component Annotation, below is the full code.
  • Student.java
    package in.sp.beans;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Student
    {
    	private String name;
    	private int rollno;
    	private String emailid;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getRollno() {
    		return rollno;
    	}
    	public void setRollno(int rollno) {
    		this.rollno = rollno;
    	}
    	public String getEmailid() {
    		return emailid;
    	}
    	public void setEmailid(String emailid) {
    		this.emailid = emailid;
    	}
    	
    	public void display()
    	{
    		System.out.println("Name : "+name);
    		System.out.println("Roll No : "+rollno);
    		System.out.println("Email Id : "+emailid);
    	}
    }
  • By using @Component annotation, Spring Container automatically creates the Bean Object.
4. Enable Component Scanning with @ComponentScan
  • Now click on src folder and create one Java Class i.e. AppConfig.java in "in.sp.config" package.





  • Note : We can provide any package name instead of "in.sp.config" and any Java Configuration File name instead of "AppConfig.java".

  • Note : Here we are providing the configurations using Annotations, Click Here for Spring Program using "Java Configuration File".

  • Below is the full code of Configuration File
  • AppConfig.java
    package in.sp.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(basePackages = "in.sp.beans")
    public class AppConfig
    {
        // No bean definitions are required here
    }
  • @Configuration is used to indicate that the class (AppConfig) is a configuration class.
  • @ComponentScan is used to tell Spring to scan for components (POJO or JavaBean Classes) in the specified package.
5. Create Main Class, Start the Container & Access the Beans
  • Right click on src folder and create new class i.e. MainApp in package name "in.sp.main".





  • Now create main method.
  • Next create Spring Container (We will use ApplicationContext as this is new Spring Container) and load the Spring Configuration File (Java file).
    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  • Access the Java Bean Object and call the method
  • Below is the full code :
    MainApp.java
    package in.sp.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import in.sp.beans.Student;
    import in.sp.config.AppConfig;
    
    public class MainApp 
    {
    	public static void main(String[] args)
    	{
            //Spring container is created
    		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    		
            //Bean object is requested from Spring Container and stored in Student reference
    		Student std = context.getBean(Student.class);
    
    		std.display();
    	}
    }
  • Now right click on the project or MainApp.java class and run the program, lets see below what happens :



  • Yes; we got the output but wait, why its showing 0 and null...!!
  • Its because we didnt initialized the bean object. For this we will use @Value annotation in POJO class as below.
  • MainApp.java
    package in.sp.beans;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Student
    {
    	@Value("Deepak")
    	private String name;
    	
    	@Value("103")
    	private int rollno;
    	
    	@Value("deepak@gmail.com")
    	private String emailid;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getRollno() {
    		return rollno;
    	}
    	public void setRollno(int rollno) {
    		this.rollno = rollno;
    	}
    	public String getEmailid() {
    		return emailid;
    	}
    	public void setEmailid(String emailid) {
    		this.emailid = emailid;
    	}
    	
    	public void display()
    	{
    		System.out.println("Name : "+name);
    		System.out.println("Roll No : "+rollno);
    		System.out.println("Email Id : "+emailid);
    	}
    }
  • Now run the program again.



  • Hurrey, this time we got the output...!!