Spring Boot MVC has brought some cool stuff for spring developers.If you had used spring3 (MVC) we had to configure internalresourceviewresolver by our self in spring configuration file.Then write web.xml and load the spring configuration file on class path.

The best part for moving to spring boot is to get rid of all the configuration stuff and just focus on your requirement.

Why Spring Boot

SpringBoot will configure InternalResourceViewResolver,dataSource automatically.

Please go through my code I have not mentioned Beans declaration. No need to mention componentscan but still boot does this job of component scanning.All thanks to annotation @SpringBootApplication.

Steps to create SpringBootMVC project.

  • Create simple Maven project name it as SpringBoot
  • see the below folder structure.Create files accordingly.Other way to create maven project is Use Spring Test Suite
  • Click File->New->Create Maven Project->
  • Spring Boot
  • Write Name of SpringBoot Project
  • Click Finish
  • Copy paste the file POM.xml 
  • Right click POM file 
  • Run As→Maven clean
  • RunAs→Maven install
  • Congrats you have got all the dependencies in your local repository.

@SpringApplication 

Why I have used annotation @SpringBootApplication because it is equivalent to the @ComponentScan ,@Configuration @EnableAutoConfiguration

Main Class

 

Controller Class

 


person.html

 




POM.xml

 


How to run SpringBoot project.

If you are using STS just run main file having @SpringBootApplication with run as spring boot app.

If you are using eclipse

  • Right click POM file 
  • Run As→Maven clean
  • RunAs→Maven install

Now go to the directory target where jar file is created.You can see it on console.

 

Open command prompt.

use command 

java jar C:\Users\601840\Desktop\SpringBoot\target\SpringBoot-1.0.1-SNAPSHOT.jar

your application is running on port 8080

Hit URL http://localhost:8080/shiv

Spring configures beans based on what dependencies we have mentioned in POM.xml.

Why I have used spring-boot-starter-thymeleaf?

  • thymeleaf is used in web(servlet) and non web environment.
  • It is java template engine for HTML5,XML,XHTML.
    • It helps you to create UI side from plain html code.

Why I have used spring-boot-starter-parent?

  • It helps for auto configurations.
  • It is having logging facility.

Why I have used spring-boot-starter-data-jpa?

  • When spring boot detects jpa dependancy along with H2.
  • It configures datasource for you.
  • It will create JpaVendorAdaptor for you.
  • So it takes away the pain of defining beans for dataource, jpa. You just need to extend your crudrepository.

Please download example at https://github.com/shivapreals/spring/blob/master/SpringBoot.zip

Further Reading http://prourls.co/s3jg

 

Leave a Reply

Your email address will not be published. Required fields are marked *