Mega Menu

Monday, April 13, 2020

SpringBoot Basics

Below data as of March 2020 and may have been changed at a later date**

IDE for development - Spring Tool Suite (STS)

Default Embedded Server - Tomcat

During a starter project creation, the pom.xml loads all the mandatory libraries with appropriate and compatible versions.
     Open pom.xml with 'Maven POM Editor' in STS and view the tab 'Effective POM' to check all the libraries included and their versions.

Key Features -
1. Auto Configuration of Dispatcher Servlet, Data source and Transaction Manager, etc.
2. Spring Boot Starters with Module Availability and Version Compatability. Example starters -
                        spring-boot-starter-parent
                        spring-boot-starter-web
                        spring-boot-starter-data-jpa
 3. Embedded Servlet Container. Eg:
                        Tomcat (default)
                         Jetty
                         Undertow
4. SpringBootActuators end points to view -
                         autoconfig
                         mappings
                         info
                         health
                         metrics
5. Class with the annotation @SpringBootApplication has the main method which will be run when the boot is executed. Example -
    @SpringBootApplication
public class SpringwebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringwebApplication.class, args);
    }

}


6. Class with annotation @SpringBootTest is the test class to execute the spring boot application.
     Example -
        package com.raj.springdatajpa;
       import static org.junit.Assert.assertEquals;
       import static org.junit.Assert.assertNotNull;
       import org.junit.Test;
       import org.junit.runner.RunWith;
       import org.springframework.beans.factory.annotation.Autowired;
       import org.springframework.boot.test.context.SpringBootTest;
       import org.springframework.test.context.junit4.SpringRunner;
       import com.raj.springdatajpa.model.Student;
       import com.raj.springdatajpa.repos.StudentRepository;

       @RunWith(SpringRunner.class)
       @SpringBootTest
       public class SpringdatajpaApplicationTests {
   
          @Autowired
          private StudentRepository stdRepo;
   
          @Test
          public void testSaveStudent() {
            Student student = new Student();
            student.setId(1l);
            student.setName("raj");
            student.setTestScore(100);
            stdRepo.save(student);
            Student savedStudent = stdRepo.findById(1l).get();
            assertEquals(savedStudent.getName(), student.getName());
       
       }
   }

                                
  7. Sample POM with spring-starter-parent
         
       


By default this may not include the dependency for junit and needs the below to be added to as to use the junit.


      


 

No comments:

Post a Comment