- Spring Boot is a Spring framework module which provides RAD (Rapid Application Development) feature to the Spring framework.
- It is highly dependent on the starter templates feature
- Starter Template
- Spring Boot starters are templates that contain a collection of all the relevant dependencies that are needed to start a particular functionality.
- For example, If you want to create a Spring WebMVC application then in a traditional setup, you would have included all required dependencies yourself. It leaves the chances of version conflict which ultimately result in more runtime exceptions.
- With String boot, to create MVC application all you need to import is spring-boot-starter-web dependency.
- We do not need to provide version information into child dependencies. All versions are resolved in relation to version of parent starter
- Bootstrap the application
- To run the application, we need to use @SpringBootApplication annotation. Behind the scenes, that’s equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan together.
- It enables the scanning of config classes, files and load them into spring context. In below example, execution start with main() method. It start loading all the config files, configure them and bootstarp the application based on application properties in application.properties file in /resources folder.
- YlbSBA.java
- To execute the application, you can run the main() method from IDE such eclipse, or you can build the jar file and execute from command prompt.
- Embedded server
- Spring boot applications always include tomcat as embedded server dependency. It means you can run the Spring boot applications from the command prompt without needling complex server infrastructure.
- You can exclude tomcat and include any other embedded server if you want. Or you can make exclude server environment altogether. It’s all configuration based.
- For example, below configuration exclude tomcat and include jetty as embedded server.
- To remove embedded tomcat and deploy in external server
- pom.xml
- Make starter -tomcat dependency to provided
@ComponentScan({ "com.ylb.web", })
@SpringBootApplication
public class YlbSBA {
public static void main(String[] args) {
SpringApplication.run(YlbSBA.class, args);
}
}
### Server port #########
server.port=8080
### Context root ########
server.contextPath=/home
$ java -jar spring-boot-demo.jar
- YLBSBA.java
- Ovrride configure methode from SpringBootServletInitializer
- Spring boot autoconfiguration
- Autoconfiguration is enabled with @EnableAutoConfiguration annotation. Spring boot auto configuration scans the classpath, finds the libraries in the classpath and then attempt to guess the best configuration for them, and finally configure all such beans.
- Actuator
- Spring Boot Actuator help you monitor and manage your application health when you push it to production.
- You can choose to manage and monitor your application by using HTTP endpoints.
- Advantages of Spring boot
- Spring boot helps in resolving dependency conflict. It identifies required dependencies and import them for you.
- It has information of compitable version for all dependencies. It minimizes the runtime classloader issues.
- It’s “opinionated defaults configuration” approach helps you in configuring most important pieces behind the scene. Override them only when you need. Otherwise everything just works, perfectly. It helps in avoiding boilerplate code, annotations and XML configurations.
- It provides embedded HTTP server Tomcat so that you can develop and test quickly.
- It has excellent integration with IDEs like eclipse and intelliJ idea.
- Spring Cloud?
- It provides microservices infrastructure like provide use services such as Service Discovery, Configuration server and Monitoring.
- Eureka is the Netflix Service Discovery Server and Client. Eureka Server is using Spring Cloud.
- @EnableEurekaServer annotation uses to create eureka server.
- @EnableDiscoveryClient annotation also allows us to query Discovery server to find miroservices.
- Spring provide smart RestTemplate for service discovery and load balancing by using @LoadBalanced annotation with RestTemplate instance.
- @EnableEurekaClient makes Micro-service as EurekaClient
@ComponentScan({ "com.ylb.web", })
@SpringBootApplication
public class YlbSBA extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(YlbSBA.class, args);
}
/**
* spring boot dedicated tomcat config step #2
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(YlbSBA.class);
}
}
No comments:
Post a Comment