29.spring Boot
29.spring Boot
Prerequisites
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide production-ready features such as metrics, health checks, and externalized configuration
What Is Spring?
Spring JDBC
Spring MVC
Spring Security
What Is Exactly Spring Boot?
Spring Boot is basically an extension of the Spring framework, which eliminates the boilerplate configurations
required for setting up a Spring application.
It takes an opinionated view of the Spring platform, which paves the way for a faster and more efficient
development ecosystem.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
Unlike Spring, Spring Boot requires only one dependency to get
a web application up and running:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
</dependency>
spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
MVC Configuration
@Override
context.setConfigLocation("com.example");
container.addListener(new ContextLoaderListener(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
We also need to add the @EnableWebMvc annotation to a @Configuration class, and
define a view-resolver to resolve the views returned from the controllers:
@EnableWebMvc
@Configuration
@Bean
InternalResourceViewResolver bean
= new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
Spring Boot only needs a couple of properties to make things
work once we add the web starter
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
There are bunch of things that spring require lot mannual time and
effort.
Example:
Configuring template engine
Spring security Configuration
Database connection
Application Bootstrap
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Spring Boot Starters
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot Annotations
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@SpringBootConfiguration
Package Structure of a Spring Boot Project
org.springframework.samples.petclinic.model
org.springframework.samples.petclinic.owner
org.springframework.samples.petclinic.system
org.springframework.samples.petclinic.vet
org.springframework.samples.petclinic.visit
Spring Boot Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>