Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Spring Boot: a Quick Introduction
1. Spring
An introduction
Roberto Casadei
Concurrent and Distributed Programming course
Department of Computer Science and Engineering (DISI)
Alma Mater Studiorum – Università of Bologna
June 16, 2018
PCD1718 Introduction Spring Boot 1/12
3. Spring » intro
What
Spring: OSS framework that makes it easy to create JVM-based enterprise apps
At its heart is an IoC container (managing beans and their dependencies)
Also, notably: AOP support
Term “Spring” also refers to the family of projects built on top of Spring Framework
Spring Boot: opinionated, CoC-approach for production-ready Spring apps
Spring Cloud: provides tools/patterns for building/deploying µservices
Spring AMQP: supports AMQP-based messaging solutions... many others...
Some History
2003 – Spring sprout as a response to the complexity of the early J2EE specs.
2006 – Spring 2.0 provided XML namespaces and AspectJ support
2007 – Spring 2.5 embraced annotation-driven configuration
Over time, the approach Java enterprise application development has evolved.
Java EE application servers – for full-stack monolithic web-apps
Spring Boot/Cloud apps – for devops/cloud-friendly apps, with embedded server
Spring WebFlux, released with Spring 5 in 2017, supports reactive-stack web apps
PCD1718 Introduction Spring Boot 3/12
4. Spring » intro
What
Spring: OSS framework that makes it easy to create JVM-based enterprise apps
At its heart is an IoC container (managing beans and their dependencies)
Also, notably: AOP support
Term “Spring” also refers to the family of projects built on top of Spring Framework
Spring Boot: opinionated, CoC-approach for production-ready Spring apps
Spring Cloud: provides tools/patterns for building/deploying µservices
Spring AMQP: supports AMQP-based messaging solutions... many others...
Some History
2003 – Spring sprout as a response to the complexity of the early J2EE specs.
2006 – Spring 2.0 provided XML namespaces and AspectJ support
2007 – Spring 2.5 embraced annotation-driven configuration
Over time, the approach Java enterprise application development has evolved.
Java EE application servers – for full-stack monolithic web-apps
Spring Boot/Cloud apps – for devops/cloud-friendly apps, with embedded server
Spring WebFlux, released with Spring 5 in 2017, supports reactive-stack web apps
PCD1718 Introduction Spring Boot 3/12
5. Spring » beans and wiring I
(Spring) Bean: application object managed by the Spring IoC container
Configuration metadata tells the Spring container how instantiate, configure,
and assemble the objects in your application.
a) XML-based metadata
b) Java-based configuration
Java-based container configuration
Factory methods in Configuration classes can be annotated with Bean
Configuration public class AppConfig {
Bean public MyService myService() { return new MyServiceImpl(); }
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
} }
PCD1718 Introduction Spring Boot 4/12
6. Spring » beans and wiring II
Spring-managed components
Stereotypes: Component, Service, Controller, Repository
Spring can automatically detect stereotyped classes and register corresponding
BeanDefinitions with the ApplicationContext, via ComponentScan
Configuration
ComponentScan(basePackages="it.unibo.beans")
public class AppConfig { }
package it.unibo.beans;
Service
public class MyService {
Autowired private ServiceA sa; // field injection
public MyService(ServiceB sb){ ... } // constructor injection
Autowired
public void setServiceC(ServiceC sc){ .. } // setter injection
}
PCD1718 Introduction Spring Boot 5/12
8. Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring-based
Applications that you can just run
It takes an opinionated view of the Spring platform and 3rd-party libraries
Reasonable convention-over-configuration for getting started quickly
It provides a light version of Spring targeted at Java-based RESTful µservices,
without the need for an external application container
It abstracts away the common REST microservice tasks (routing to business
logic, parsing HTTP params from the URL, mapping JSON to/from POJOs), and
lets the developer focus on the service business logic.
Supported embedded containers: Tomcat 8.5, Jetty 9.4, Undertow 1.4
PCD1718 Introduction Spring Boot 7/12
9. Build configuration
Build plugins
spring-boot-maven-plugin for Maven
spring-boot-gradle-plugin1
for Gradle
Dependencies (cf., group ID org.springframework.boot)
Starters are a set of convenient dependency descriptors to get a project up and
running quickly.
spring-boot-starter-web: starter for web, RESTful / MVC apps (Tomcat as
default container)
spring-boot-starter-actuator: gives production-ready features for app
monitoring/management
Run
Maven tasks: spring-boot:run, package
Gradle tasks: bootRun, bootJar
1https:
//docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/
PCD1718 Introduction Spring Boot 8/12
10. Hello Spring Boot
SpringBootApplication // tells Boot this is the bootstrap class
public class MyApp {
public static void main(String[] args){
SpringApplication.run(MyApp.class, args);
}
}
SpringBootApplication also implies
– EnableAutoConfiguration: tells Spring Boot to "guess" config by classpath
– ComponentScan: tells Spring to look for components
RestController RequestMapping(value="/app")
public class MyController {
RequestMapping(value="/hello/{name}", method = RequestMethod.GET)
public String hello( PathVariable("name") String name){
return "Hello, " + name;
}
}
Endpoint: http://localhost:8080/app/hello/Boot
Actuator also exposes http://localhost:8080/actuator/health
PCD1718 Introduction Spring Boot 9/12
11. A path for learning Spring Boot I
Spring Guides: https://spring.io/guides
Do-It-Yourself
Building an Application with Spring Boot
https://spring.io/guides/gs/spring-boot/
Use RestController and RequestMapping to add endpoints.
Create a SpringBootApplication class with main method.
Consuming a RESTful Web Service
https://spring.io/guides/gs/consuming-rest/
You use RestTemplate to make calls to RESTful services
You can use RestTemplateBuilder to build RestTemplate Beans as needed.
Building a Reactive RESTful Web Service (with Spring WebFlux)
https://spring.io/guides/gs/reactive-rest-service/
(Optional) Creating Asynchronous Methods
https://spring.io/guides/gs/async-method/
Declare Async methods (so they’ll run on a separate thread) that return
CompletableFuture<T>.
Annotate your app with EnableAsync and define an Executor Bean
PCD1718 Introduction Spring Boot 10/12
12. A path for learning Spring Boot II
(Optional) Messaging with RabbitMQ
https://spring.io/guides/gs/messaging-rabbitmq/
(Optional) Messaging with Redis
https://spring.io/guides/gs/messaging-redis/
Start redis: redis-server (default on port 6379)
You need to configure (i) a connection factory to connect to the Redis server; (ii) a
message listener container for registering receivers; and (iii) a Redis template to
send messages.
If the listener is a POJO, it needs to be wrapped in a MessageListenerAdapter.
(Optional) Accessing Data Reactively with Redis
https://spring.io/guides/gs/spring-data-reactive-redis/
Create a configuration class with Spring Beans supporting reactive Redis
operations—cf., ReactiveRedisOperations<K,V>
Inject ReactiveRedisOperations<K,V> to interface with Redis
(Optional) Scheduling Tasks
https://spring.io/guides/gs/scheduling-tasks/
EnableScheduling + Scheduled on a Component’s method.
PCD1718 Introduction Spring Boot 11/12