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

Spring First Program using Java 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
  4. Create "Java Configuration File"
  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
  • Create one POJO class i.e. "Student" in package "in.sp.beans" and create one display() method to print Student details.





  • Student.java
    package in.sp.beans;
    
    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);
    	}
    }
4. Create "Java Configuration File"
  • Now click on src folder and create one Java Class i.e. AppConfig.java (Java Configuration File) 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 Java file, Click Here for Spring Program using "XML Configuration File".

  • Below is the full code of Java Configuration File
  • AppConfig.java
    package in.sp.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import in.sp.beans.Student;
    
    @Configuration
    public class AppConfig
    {
    	@Bean
        public Student stdId()
        {
    		Student std = new Student();
    		
    		std.setName("Deepak");
    		std.setRollno(102);
    		std.setEmailid("deepak@gmail.com");
    		
            return std;
        }
    }
  • @Configuration is used to indicate that the class (AppConfig) is a configuration class in Spring where we can provide different Spring Configurations (bean configurations).
  • @Bean is used on methods within a @Configuration class to specify that the return value of the method should be registered as a Spring bean in the application context.
  • Spring Container will read the above Java Configuration file and will create one Student class bean object, then set the properties name, rollno and email with respective values.
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, we will get the output as shown below :



  • Hurrey, Program completed and we got the output...!!