SlideShare une entreprise Scribd logo
1  sur  20
By : Jaspereet Juneja & Sourabh Aggarwal
Date : 30 July 2015
SPRING BOOT
What is Spring Boot?
● Spring Boot is a approach to develop Spring based application
with very less or no configuration.
● It leverages existing Spring projects as well as Third party
projects to develop production ready applications.
● It provides a set of Starter Pom’s, gradle etc.. build files which
one can use to add required dependencies and also facilitate
auto configuration. Depending on the libraries on its classpath,
Spring Boot automatically configures required classes.
● For example to interact with DB, if there is Spring Data libraries
on class path then it automatically sets up connection to DB
along with the Data Source class.
Spring Boot focus point?
Why to use spring boot?
➔ Create stand-alone Spring applications
➔ Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
➔ Provide opinionated 'starter' POMs to simplify your Maven configuration
➔ Automatically configure Spring whenever possible
➔ Provide production-ready features such as metrics, health checks and
externalized configuration
➔ Absolutely no code generation and no requirement for XML configuration.
Spring Boot Modules?
Boot
Autoconfigure
Starters
CLI
Actuator
Tools
Samples
Spring Boot Modules?
● Spring Boot - main library supporting the other parts of Spring Boot
● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation
creates a whole Spring context
● Spring Boot Starters - a set of convenient dependency descriptors that
you can include in your application.
● Spring Boot CLI - compiles and runs Groovy source as a Spring
application
● Spring Boot Actuator - common non-functional features that make an app
instantly deployable and supportable in production
● Spring Boot Tools - for building and executing self-contained JAR and
WAR archives
● Spring Boot Samples - a wide range of sample apps
Spring Boot Actuator?
● It’s a Spring Boot module that immediately gives your Spring-based web applications basic health
check and monitoring interfaces. To use it just add Maven dependency spring-boot-starter-actuator.
● If you now recompile and restart your service, you’ll notice that a lot more endpoints are being
mapped (this is printed to the log during startup). Some of these are:
– /health – returns “ok” as text/plain content which is useful for simple service monitoring
– /env – check environment configuration, property file and command line argument overrides, active profiles
– /metrics – basic statistics on your service endpoints (e.g. hit count, error count)
– /dump – thread dump
– /trace – the latest HTTP request/response pairs
● You can access/modify (or change completely if you wish) the behavior for most of these. For
example, if you want to add a custom metric, just inject a MetricRepository in your business beans,
and start using it, it’ll get exposed via the Actuator /metrics interface.
Spring Boot over Spring
● Opinionated Convention Over Configuration
● Automatic Configuration
● Starter + Example Builds
● Standalone Apps
● No XML!
● Embedded Containers
● Metrics (Actuator)
● Groovy
Hello World(Java)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class MyApplication {
@RequestMapping("/hello")
public String sayHello() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@EnableAutoConfiguration
● Attempts to auto-configure your application
● Backs off as you define your own beans
@Configuration
@EnableAutoConfiguration
public class MyApplication {
}
Starter POMs
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
● Standard Maven POMs
● Define dependencies that we recommend
● Parent optional
● Available for web, batch, integration, data, amqp, aop, jdbc, ...
● e.g. data = hibernate + spring-data + JSR 303
Packaging For Production
Maven plugin (using spring-boot-starter-parent):
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
$ mvn package
Annotations
We are relying on Spring’s annotation and package-scan based
approach to configure our controller
● @EnableAutoConfiguration is specific to Spring Boot and declaring it
will result in a lot of pre-defined configuration pulled to our Spring
Application Context (like the embedded Tomcat container)
● SpringApplication is another Boot-specific component, it’s the default
entry point for most of the Spring Boot applications and will take care of
the Spring Application Context for us.
● And that is all you actually need. At this point you should be able to start
this application from your favourite IDE, launching class
HelloConfiguration. Then visit http://localhost:8080/hello which should
result in “Hello World!” being displayed in your browser.
Problems?
Although most features that Spring Boot gives you are extremely useful and work very well
immediately, we encountered a few cases where a bit of caution is recommended:
– Spring Boot will import and use Logback and SLF4J as default loggers. If you want to use something else (e.g.
Log4J) or one of your dependencies transitively imports it, you need to be careful and use the appropriate logger
bridges. Some combinations may result in your applications not starting at all. Maven exclusions for transitive
dependencies will come handy.
– The Spring Boot Maven Builder packages executable jar files in a specific way, effectively adding multiple levels of
nested jar files. This is a problem if your libraries want to extract contents from the nested jar files as Java won’t be
able to resolve that resource path. Consider using a different packaging mechanism like Shade or AppAssembler
in such cases. Spring Boot Maven Parent comes with a basic configuration for Shade, but the preferred choice
should be the Boot-builder.
– We advise some level of caution if you want to use embedded application servers and JSPs with Spring Boot,
there may be some limitations.
Overriding default configuration
● Using property files?
● Overriding with command line arguments?
● Support for Spring profiles?
● Overriding default beans?
Using property files?
●
By default Spring Boot will look for a property file in the package root directory called
application.properties, this is a good place to customize your application. By Maven
conventions, place this file into the src/main/resources directory so your build artefacts will be
generated correctly. For example let’s set the contents to:
server.port=11000
● This will cause the embedded Tomcat to listen on port 11000 instead of 8080. If you now
restart your service, you should use http://localhost:11000/hello to get access.
●
Finding out what is given by default and what you can override is probably the biggest
problem with Spring Boot at the moment. There is no comprehensive documentation about all
the options, but the code in org.springframework.boot.autoconfigure.* packages is a good
starting point. (e.g. server.port is bound to a field in
org.springframework.boot.autoconfigure.web.ServerProperties as of Boot 1.0.0.RC3).
Overriding with command line
arguments?
● If you need more execution-specific parameter use, you can do
it. The usual convention of the property overriding chain: defaults
< property files < Java VM arguments is still valid, but Spring
Boot will also give you Spring’s command-line argument
property source: with double dashes (“–”) arguments when
executing your application, you can specify property overrides:
$ java -jar spring-boot-example-0.0.1-SNAPSHOT.jar
--server.port=12000
● As you probably have already figured out, this will result in your
webserver listening on port 12000 and your application available
on http://localhost:12000/hello
Support for Spring profiles?
● Spring Boot actively supports the usage of Spring Profiles.
First, to activate a profile, you can use the double-dash
command line argument syntax: –spring.profiles.active and
list those that you want activated. Additionally, you can name
your property files in the following way:
application-{profile}.properties
● And only those that match one of the active profiles will get
loaded.
● Regarding your Spring beans, you can rely on the @Profile
annotation to work as expected.
Overriding default beans
● When pulling a spring-boot-starter project as a dependency,
Boot will declare the most commonly needed beans. For
example, in case of a web project, you will get a
DispatcherServlet without having to do anything (usually, you
can configure these default beans with externalized
properties).
● In the majority of the cases this will be sufficient. However, if
you want to have more control over these beans, just declare
them as you normally would and your beans will override the
ones given by Boot (in fact, Boot won’t even instantiate any of
its default beans if you have an overriding bean).
Xke spring boot

Contenu connexe

Tendances

Tendances (20)

Spring boot
Spring bootSpring boot
Spring boot
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Spring boot
Spring bootSpring boot
Spring boot
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 

Similaire à Xke spring boot

Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 
Module 6 _ Spring Boot for java application to begin
Module 6 _ Spring Boot for java application to beginModule 6 _ Spring Boot for java application to begin
Module 6 _ Spring Boot for java application to begin
Deepakprasad838637
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
🎤 Hanno Embregts 🎸
 
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
Carlos Sanchez
 

Similaire à Xke spring boot (20)

Spring boot for buidling microservices
Spring boot for buidling microservicesSpring boot for buidling microservices
Spring boot for buidling microservices
 
Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)Rediscovering Spring with Spring Boot(1)
Rediscovering Spring with Spring Boot(1)
 
Module 6 _ Spring Boot for java application to begin
Module 6 _ Spring Boot for java application to beginModule 6 _ Spring Boot for java application to begin
Module 6 _ Spring Boot for java application to begin
 
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
 
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfdokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot Whirlwind Tour
Spring Boot Whirlwind TourSpring Boot Whirlwind Tour
Spring Boot Whirlwind Tour
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!
 
Spring boot
Spring bootSpring boot
Spring boot
 
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
 
SpringBootCompleteBootcamp.pptx
SpringBootCompleteBootcamp.pptxSpringBootCompleteBootcamp.pptx
SpringBootCompleteBootcamp.pptx
 
dokumen.tips_introduction-to-spring-boot-58bb649a21ce5.pptx
dokumen.tips_introduction-to-spring-boot-58bb649a21ce5.pptxdokumen.tips_introduction-to-spring-boot-58bb649a21ce5.pptx
dokumen.tips_introduction-to-spring-boot-58bb649a21ce5.pptx
 
Spring Boot & Actuators
Spring Boot & ActuatorsSpring Boot & Actuators
Spring Boot & Actuators
 
Springboot2 postgresql-jpa-hibernate-crud-example with test
Springboot2 postgresql-jpa-hibernate-crud-example with testSpringboot2 postgresql-jpa-hibernate-crud-example with test
Springboot2 postgresql-jpa-hibernate-crud-example with test
 
Spring competitive tests
Spring competitive testsSpring competitive tests
Spring competitive tests
 
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
 
Apache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentationApache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentation
 
Using Maven2
Using Maven2Using Maven2
Using Maven2
 

Plus de sourabh aggarwal (6)

Mongo production Sharded cluster
Mongo production Sharded clusterMongo production Sharded cluster
Mongo production Sharded cluster
 
Mule Complete Training
Mule Complete TrainingMule Complete Training
Mule Complete Training
 
Spring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentationSpring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentation
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
 
Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
 
Liferay with xebia
Liferay with xebiaLiferay with xebia
Liferay with xebia
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Xke spring boot

  • 1. By : Jaspereet Juneja & Sourabh Aggarwal Date : 30 July 2015 SPRING BOOT
  • 2. What is Spring Boot? ● Spring Boot is a approach to develop Spring based application with very less or no configuration. ● It leverages existing Spring projects as well as Third party projects to develop production ready applications. ● It provides a set of Starter Pom’s, gradle etc.. build files which one can use to add required dependencies and also facilitate auto configuration. Depending on the libraries on its classpath, Spring Boot automatically configures required classes. ● For example to interact with DB, if there is Spring Data libraries on class path then it automatically sets up connection to DB along with the Data Source class.
  • 4. Why to use spring boot? ➔ Create stand-alone Spring applications ➔ Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) ➔ Provide opinionated 'starter' POMs to simplify your Maven configuration ➔ Automatically configure Spring whenever possible ➔ Provide production-ready features such as metrics, health checks and externalized configuration ➔ Absolutely no code generation and no requirement for XML configuration.
  • 6. Spring Boot Modules? ● Spring Boot - main library supporting the other parts of Spring Boot ● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation creates a whole Spring context ● Spring Boot Starters - a set of convenient dependency descriptors that you can include in your application. ● Spring Boot CLI - compiles and runs Groovy source as a Spring application ● Spring Boot Actuator - common non-functional features that make an app instantly deployable and supportable in production ● Spring Boot Tools - for building and executing self-contained JAR and WAR archives ● Spring Boot Samples - a wide range of sample apps
  • 7. Spring Boot Actuator? ● It’s a Spring Boot module that immediately gives your Spring-based web applications basic health check and monitoring interfaces. To use it just add Maven dependency spring-boot-starter-actuator. ● If you now recompile and restart your service, you’ll notice that a lot more endpoints are being mapped (this is printed to the log during startup). Some of these are: – /health – returns “ok” as text/plain content which is useful for simple service monitoring – /env – check environment configuration, property file and command line argument overrides, active profiles – /metrics – basic statistics on your service endpoints (e.g. hit count, error count) – /dump – thread dump – /trace – the latest HTTP request/response pairs ● You can access/modify (or change completely if you wish) the behavior for most of these. For example, if you want to add a custom metric, just inject a MetricRepository in your business beans, and start using it, it’ll get exposed via the Actuator /metrics interface.
  • 8. Spring Boot over Spring ● Opinionated Convention Over Configuration ● Automatic Configuration ● Starter + Example Builds ● Standalone Apps ● No XML! ● Embedded Containers ● Metrics (Actuator) ● Groovy
  • 9. Hello World(Java) import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class MyApplication { @RequestMapping("/hello") public String sayHello() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
  • 10. @EnableAutoConfiguration ● Attempts to auto-configure your application ● Backs off as you define your own beans @Configuration @EnableAutoConfiguration public class MyApplication { }
  • 11. Starter POMs <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ● Standard Maven POMs ● Define dependencies that we recommend ● Parent optional ● Available for web, batch, integration, data, amqp, aop, jdbc, ... ● e.g. data = hibernate + spring-data + JSR 303
  • 12. Packaging For Production Maven plugin (using spring-boot-starter-parent): <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> $ mvn package
  • 13. Annotations We are relying on Spring’s annotation and package-scan based approach to configure our controller ● @EnableAutoConfiguration is specific to Spring Boot and declaring it will result in a lot of pre-defined configuration pulled to our Spring Application Context (like the embedded Tomcat container) ● SpringApplication is another Boot-specific component, it’s the default entry point for most of the Spring Boot applications and will take care of the Spring Application Context for us. ● And that is all you actually need. At this point you should be able to start this application from your favourite IDE, launching class HelloConfiguration. Then visit http://localhost:8080/hello which should result in “Hello World!” being displayed in your browser.
  • 14. Problems? Although most features that Spring Boot gives you are extremely useful and work very well immediately, we encountered a few cases where a bit of caution is recommended: – Spring Boot will import and use Logback and SLF4J as default loggers. If you want to use something else (e.g. Log4J) or one of your dependencies transitively imports it, you need to be careful and use the appropriate logger bridges. Some combinations may result in your applications not starting at all. Maven exclusions for transitive dependencies will come handy. – The Spring Boot Maven Builder packages executable jar files in a specific way, effectively adding multiple levels of nested jar files. This is a problem if your libraries want to extract contents from the nested jar files as Java won’t be able to resolve that resource path. Consider using a different packaging mechanism like Shade or AppAssembler in such cases. Spring Boot Maven Parent comes with a basic configuration for Shade, but the preferred choice should be the Boot-builder. – We advise some level of caution if you want to use embedded application servers and JSPs with Spring Boot, there may be some limitations.
  • 15. Overriding default configuration ● Using property files? ● Overriding with command line arguments? ● Support for Spring profiles? ● Overriding default beans?
  • 16. Using property files? ● By default Spring Boot will look for a property file in the package root directory called application.properties, this is a good place to customize your application. By Maven conventions, place this file into the src/main/resources directory so your build artefacts will be generated correctly. For example let’s set the contents to: server.port=11000 ● This will cause the embedded Tomcat to listen on port 11000 instead of 8080. If you now restart your service, you should use http://localhost:11000/hello to get access. ● Finding out what is given by default and what you can override is probably the biggest problem with Spring Boot at the moment. There is no comprehensive documentation about all the options, but the code in org.springframework.boot.autoconfigure.* packages is a good starting point. (e.g. server.port is bound to a field in org.springframework.boot.autoconfigure.web.ServerProperties as of Boot 1.0.0.RC3).
  • 17. Overriding with command line arguments? ● If you need more execution-specific parameter use, you can do it. The usual convention of the property overriding chain: defaults < property files < Java VM arguments is still valid, but Spring Boot will also give you Spring’s command-line argument property source: with double dashes (“–”) arguments when executing your application, you can specify property overrides: $ java -jar spring-boot-example-0.0.1-SNAPSHOT.jar --server.port=12000 ● As you probably have already figured out, this will result in your webserver listening on port 12000 and your application available on http://localhost:12000/hello
  • 18. Support for Spring profiles? ● Spring Boot actively supports the usage of Spring Profiles. First, to activate a profile, you can use the double-dash command line argument syntax: –spring.profiles.active and list those that you want activated. Additionally, you can name your property files in the following way: application-{profile}.properties ● And only those that match one of the active profiles will get loaded. ● Regarding your Spring beans, you can rely on the @Profile annotation to work as expected.
  • 19. Overriding default beans ● When pulling a spring-boot-starter project as a dependency, Boot will declare the most commonly needed beans. For example, in case of a web project, you will get a DispatcherServlet without having to do anything (usually, you can configure these default beans with externalized properties). ● In the majority of the cases this will be sufficient. However, if you want to have more control over these beans, just declare them as you normally would and your beans will override the ones given by Boot (in fact, Boot won’t even instantiate any of its default beans if you have an overriding bean).