Spring Boot End of WAR GeorgeAlexandru Vlad Agenda


Spring Boot - End of WAR? George-Alexandru Vlad

Agenda • What is Spring Boot? • Goals and features • Demo

What is Spring Boot? Framework developed on top of Spring to ease the bootstrapping and development of new Spring based applications that you can "just run“ with a minimum fuss

Goals • Provide a tool for getting started very quickly with Spring • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults • Provide a range of non-functional features that are common to large classes of projects (e. g. embedded servers, security, metrics, health checks, externalized configuration) • Absolutely no code generation and no requirement for XML configuration

Goals • Provide a tool for getting started very quickly with Spring • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults • Provide a range of non-functional features that are common to large classes of projects (e. g. embedded servers, security, metrics, health checks, externalized configuration) • Absolutely no code generation and no requirement for XML configuration

• Build web applications as self-contained JARs (no need to deploy WAR files on Application Servers, use embedded Tomcat, Jetty or Undertow) buildscript { repositories { maven. Central() } dependencies { classpath("org. springframework. boot: spring-boot-gradle-plugin: 1. 3. 6. RELEASE") } } apply plugin: 'java' apply plugin: 'spring-boot‘ spring. Boot { main. Class = 'com. demo. Application' } repositories { maven. Central() } dependencies { compile("org. springframework. boot: spring-boot-starter-web") }

• Simplify your Maven configuration by providing opinionated 'starter' POMs • Modules • Spring Boot Starters • Spring Boot CLI

Spring Boot Starters module • a set of convenient dependency descriptors that you can include in your application

• spring-boot-starter-web POM sample: <? xml version="1. 0" encoding="UTF-8"? > <project> <artifact. Id>spring-boot-starter-web</artifact. Id> <name>Spring Boot Web Starter</name> <description>Spring Boot Web Starter</description> <dependencies> <dependency> <group. Id>org. springframework. boot</group. Id> <artifact. Id>spring-boot-starter-tomcat</artifact. Id> </dependency> <group. Id>org. springframework</group. Id> <artifact. Id>spring-webmvc</artifact. Id> </dependency>. . . </dependencies> </project>

Spring Boot CLI module • command line tool that compiles and runs Groovy source as Spring application app. groovy @Rest. Controller class This. Will. Actually. Run { @Request. Mapping("/") String home() { "Hello World!" } } Run it from a shell: spring run app. groovy

Goals • Provide a tool for getting started very quickly with Spring • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults • Provide a range of non-functional features that are common to large classes of projects (e. g. embedded servers, security, metrics, health checks, externalized configuration) • Absolutely no code generation and no requirement for XML configuration

• @Enable. Auto. Configuration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings • Modules • Spring Boot Autoconfigure

Spring Boot Autoconfigure module • tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have • e. g. tomcat-embedded. jar found in classpath => Tomcat. Embedded. Servlet. Container. Factory instance created (unless you have defined your own Embedded. Servlet. Container. Factory bean).

Goals • Provide a tool for getting started very quickly with Spring • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults • Provide a range of non-functional features that are common to large classes of projects (e. g. embedded servers, security, metrics, health checks, externalized configuration) • Absolutely no code generation and no requirement for XML configuration

• Provide Rest Endpoints for monitoring and managing your application (metrics, health checks, shutdown etc. ) • Modules • Spring Boot Actuator

Spring Boot Actuator module • common non-functional features that make an app instantly deployable and supportable in production • Endpoints • beans - displays a complete list of all the Spring beans in your application • health - shows application health information • metrics • shutdown - allows the application to be gracefully shutdown (not enabled by default) • trace - displays trace information (by default the last 100 HTTP requests)

Spring Boot Actuator module • Predefined Health Indicators • Disk. Space. Health. Indicator - Checks for low disk space • Data. Source. Health. Indicator - Checks that a connection to Data. Source can be obtained • Mail. Health. Indicator - Checks that a mail server is up

Spring Boot Actuator module Define own Health Indicators (implement Health. Indicator interface) import org. springframework. boot. actuate. health. Health; import org. springframework. boot. actuate. health. Health. Indicator; @Component public class My. Health. Indicator implements Health. Indicator { @Override public Health health() { int error. Code = check(); // perform some specific health check if (error. Code != 0) { return Health. down(). with. Detail("Error Code", error. Code). build(); } return Health. up(). build(); } }

Spring Boot Actuator module • Predefined System Metrics • • • The amount of free memory in KB (mem. free) • Class load information (classes, classes. loaded, classes. unloaded) The system uptime in milliseconds (uptime) Heap information in KB (heap, heap. committed, heap. init, heap. used)

Spring Boot Actuator module Define your own Metrics (Counter. Service bean provides increment, decrement and reset methods ) import org. springframework. boot. actuate. metrics. Counter. Service; @Service public class My. Service { @Autowired private final Counter. Service counter. Service; public void example. Method() { this. counter. Service. increment("services. system. myservice. invoked"); } }

Spring Boot Actuator module Export Metrics to JMX (metrics are exported as MBeans to the local server) @Bean @Export. Metric. Writer metric. Writer(MBean. Exporter exporter) { return new Jmx. Metric. Writer(exporter); }

Trace Endpoint sample [{ "timestamp": 1394343677415, "info": { "method": "GET", "path": "/trace", "headers": { "request": { "Accept": "text/html, application/xhtml+xml, application/xml; q=0. 9, */*; q=0. 8", "Connection": "keep-alive", "User-Agent": "Mozilla/5. 0 Gecko/Firefox", "Accept-Language": "en-US, en; q=0. 5", "Cookie": "_ga=GA 1. 1. 827067509. 1390890128; . . . " "Authorization": "Basic. . . ", "Host": "localhost: 8080" }, "response": { "Strict-Transport-Security": "max-age=31536000 ; include. Sub. Domains", "X-Application-Context": "application: 8080", "Content-Type": "application/json; charset=UTF-8", "status": "200" } }]

Externalized Configuration • properties files, YAML files, environment variables and command-line arguments can be used to externalize configuration • application. properties file (packaged in jar) server. port=8080 server. context. Path=/application-context-path endpoints. enabled=false endpoints. info. enabled=true

Externalized Configuration • Override bundled configuration (when launching Java process ) --spring. config. location=/etc/user/application. properties • Externalized application. properties file server. port=8181 server. context. Path=/overridden-context-path endpoints. enabled=true

Externalized Configuration • Enable sensitive Endpoints endpoints. beans. sensitive=false endpoints. shutdown. enabled=true • Securing sensitive Endpoints (‘Spring Security’ module dependecy) security. user. name=admin security. user. password=secret management. security. roles=SUPERUSER

Goals • Provide a tool for getting started very quickly with Spring • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults. • Provide a range of non-functional features that are common to large classes of projects (e. g. embedded servers, security, metrics, health checks, externalized configuration). • Absolutely no code generation and no requirement for XML configuration.

• Servlets from web. xml translated into Spring Boot Configuration <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax. faces. webapp. Faces. Servlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*. jsf</url-pattern> </servlet-mapping> @Bean public Servlet. Registration. Bean faces. Servlet. Registration(){ Servlet. Registration. Bean registration = new Servlet. Registration. Bean(new Faces. Servlet(), "*. jsf"); registration. set. Name("Faces. Servlet"); registration. set. Load. On. Startup(1); return registration; }

• Filters from web. xml translated into Spring Boot Configuration <filter> <filter-name>char. Encoding. Filter</filter-name> <filter-class>org. springframework. web. filter. Character. Encoding. Filter </filter-class> </filter> <filter-mapping> <filter-name>char. Encoding. Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> @Bean public Filter. Registration. Bean char. Encoding. Filter. Registration(){ Filter. Registration. Bean registration = new Filter. Registration. Bean( new Character. Encoding. Filter("UTF-8", true)); registration. set. Name("char. Encoding. Filter"); registration. set. Url. Patterns(Immutable. List. of("/*")); return registration; }

• Context parameters from web. xml translated into Spring Boot Configuration <context-param> <param-name>javax. faces. STATE_SAVING_METHOD</paramname> <param-value>server</param-value> </context-param> @Bean public Servlet. Context. Initializer initializer(){ return servlet. Context -> { servlet. Context. set. Init. Parameter("javax. faces. STATE_SAVING_METHOD“ , "server"); }; }

• Mime mappings defined in web. xml translated into Spring Boot Configuration <mime-mapping> <extension>eot</extension> <mime-type>application/vnd. ms-fontobject</mime-type> </mime-mapping> @Bean public Embedded. Servlet. Container. Customizer container. Customizer(){ return container -> { Mime. Mappings mappings = new Mime. Mappings(Mime. Mappings. DEFAULT); mappings. add("eot", "application/vnd. ms-fontobject"); container. set. Mime. Mappings(mappings); }; }

DEMO

Q&A?
- Slides: 33