-
Notifications
You must be signed in to change notification settings - Fork 249
Home
O’Reilly Media course for Safari Books Online
Ken Kousen
[email protected]
@kenkousen
This document is for notes that come up during class. It’s also an easy way to share code and other materials.
Exercises link: http://www.kousenit.com/springboot/
GitHub repo for solutions: https://github.com/kousen/spring-and-spring-boot
I will go through each exercise during class. We don’t have enough time to do all of the exercises together during class, but once you’ve seen them, you should be able to follow the instructions on your own without too much trouble. The course materials and this document will be available indefinitely, so you can try them any time.
Set-up info:
-
Java 8 or Java 11 (solutions use Java 11, but exercises work with either)
-
Spring 5 (requires Java 8)
-
Spring Boot 2.1.2
Tip
|
We will use the Spring Initializr to generate apps, so the only requirement for installation is Java 8. |
IDES:
-
IntelliJ IDEA
-
Community edition (free) only supports Gradle and Java; no specific Spring functionality
-
Ultimate edition has many Spring and Spring Boot capabilities
-
-
Spring Tool Suite (STS) from Pivotal is based on Eclipse
Everything else is self-contained, including the database. The examples will use the H2 database, which is file-based and can be created by the Spring Initializr. We will discuss how to generalize to other databases.
While Spring works just fine with Maven, I personally prefer Gradle and am much more comfortable solving problems with it. You do not need to install Gradle yourself. Instead, each project will contain what is called a Gradle wrapper, which is a script that will download and install Gradle when first executed. The wrapper will install Gradle into ~/.gradle/wrapper/dists
, where the tilde (~) represents your home directory. You can always delete the installation later — no changes are made to your system environment or registry.
If you have a proxy between you and the Internet, you’ll need to tell Gradle to use it. To do so, add a file called gradle.properties
in the ~/.gradle
folder (creating it if necessary) with the following settings:
systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
systemProp.https.proxyHost=www.somehost.org
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost
Alternatively, you can add this file to each project’s root. That will work, but if you save it into ~/.gradle
, the settings will apply to every Gradle build on your system.
We’ll go over how to use Gradle during the class. If you prefer to use Maven, you’re welcome to do so, but if you run into any difficulties you’ll have to solve them yourself.
In general, Spring provides a managed infrastructure for your app. That means you ask for services that Spring provides
You tell Spring which "beans" you want it to manage. Spring then "injects" when where requested. A "bean" to Spring is just a Java POJO (a Java class with properties)
When Spring starts up, it populates application context, which is the collection of Spring-managed beans at any given moment.
@SpringBootApplication
annotation is composed of:
-
@SpringBootConfiguration
→ can configure Spring beans using "Java configuration" files -
@EnableAutoConfiguration
→ adds beans based on their presence on the classpath -
@ComponentScan
→ Searches Java classes from the current package down for any annotated Spring-managed beans
A Spring-managed bean is declared in the metadata, which is provided one of three ways:
-
In an XML file
-
Annotated with
@Component
-
Configured in a Java config file (annotated with
@Configuration
)
A Spring bean is just a POJO that Spring will "manage"
-
Spring instantiates the bean and supply it on demand
-
Spring "injects" it as a "dependency"
We add a particular type as an attribute of a class, and Spring will assign it a value.
Spring instantiates and configures all the beans and adds them to the Application Context, which is the so-called "lightweight" Spring container
Note
|
The application context is the collection of Spring-managed beans |
When Spring starts:
-
it reads all the bean definitions from all sources
-
it post-processes the bean definitions, using any wildcards supplied, if necessary, subtituting values from properties files (like
application.properties
orapplication.yml
) -
it instantiates all the beans and adds them to the application context
-
finally, it generates any necessary proxies to provide the services
Autowiring is asking for a bean in the application context. If there is exactly one, Spring will provide a reference to it. The @Autowired
annotation can be applied to constructors (preferred), setters, or fields (discouraged, but necessary in some cases)
The @ComponentScan
annotation tells Spring to search the hierarchy starting at the current package for any class annotated with @Component
and manage that bean. Spring provides several annotations based on @Component
:
-
@Configuration
-
@Service
-
@Repository
-
@Controller
We use the more specific annotations to communicate the purpose of the class.
The four "principles" of REST are:
-
Addressable resources — every resource has its own address (i.e., its own URL)
-
Content negotiation — client specifies the form of the response (JSON, XML, plain, …)
-
Uniform interface — only verbs allowed are the HTTP verbs (GET, POST, PUT, DELETE, …)
-
Hypermedia — HATEOAS: Hypermedia As The Engine Of Application State
-
Learning Path: Reactive Spring: https://learning.oreilly.com/learning-paths/learning-path-reactive/9781492057215/
-
Learning Path: Spring and Spring Boot https://learning.oreilly.com/learning-paths/learning-path-spring/9781492055334/
-
Learning Spring Boot 2.0, Greg Turnquist (Packt Publishing) https://www.safaribooksonline.com/library/view/learning-spring-boot/9781786463784/
-
Spring in Action, 5th edition, Craig Walls (Manning) https://learning.oreilly.com/library/view/spring-in-action/9781617294945/