失效链接处理 |
reactive-spring-josh long PDF 下载
本站整理下载:
相关截图:
主要内容:
8.1. Type Inference
Java 11 and later support a new form of type inference where types can be inferred, given sufficient
information on the right-side of an assignment, and then dropped from the definition on the left.
Basically:
Customer customer = new Customer(1L, "Jane");
---
is easily defined as: [source,java]
var customer = new Customer(1L, "Jane"); ---
8.2. Function Interfaces, Lambdas and Method
References
Java 8 and later support lambdas. A lambda is to method definitions what anonymous classes are to
class definitions. They let us treat functions as a first-class citizen. Except that in Java, lambdas are
slightly limited; they’re not first class citizens. But, close enough! They must have return values and
input parameters compatible with the signature of the sole abstract method on an interface. This is
called a functional interface. (Interfaces may have default, non-abstract methods where the
implementation is provided in the interface.)
Let’s suppose you had the following funtional interface:
A functional interface
interface MyHandler {
String handle(int one, Date two); }
This can be expressed in Java as:
10
Downloaded from https://www.studycrux.com
Reactive Spring
MyHandler handler = (one, two) -> "Hello";
Any parameter in any method of type MyHandler can be given as a lambda, as well.
void process (MyHandler mh) { ... } ...
process((one, two) -> "Hello");
If you’ve already got a method you’d like to use that takes an int and returns a String, it would seem
pointless to express a lambda that then just forwards its input parameters to that method. You can cut
out the intermediate lambda and point at that method using a method reference. All modern JVM
languages (even Java) support method references.
class Delegate {
String pleaseHandle(int one, Date two) {
return one + ":" + two; }}...
Delegate delegate = new Delegate();
process(delegate::pleaseHandle);
11
Downloaded from https://www.studycrux.com
Chapter 9. Bootstrap
In order to appreciate reactive programming in the context of the larger ecosystem we’ll need to learn
about Spring. What is Spring? What is dependency injection? Why do we use Spring Boot over (or in
addition to?) Spring Framework? This chapter is a primer on the basics. We don’t talk about reactive
programming in this chapter at all. If you already know the fundamentals of dependency injection,
inversion-of-control, Java configuration, Spring application contexts, aspect-oriented programming
(AOP), and Spring Boot auto-configuration then by all means skip this chapter!
For the rest of us, we’re going to demonstrate some key concepts by building something. We’ll begin
our journey, as usual, at my second favorite place on the internet, the Spring Initializr - Start dot Spring
dot IO. Our goal is to build a trivial application. I specified rsb in the Group field and bootstrap in the
Artifact field, though please feel free to select whatever you’d like. Select the following dependencies
using the combobox text field on the bottom right of the page where it says Search for dependencies:
DevTools, Web, H2, JDBC, Actuator and Lombok.
Devtools lets us restart the application by running our IDE’s build command. No need to restart the
entire JVM process. This lets us iterate quicker and see changes in the compiled code quicker. It also
starts up a Livereload-protocol server. There are some nice browser plugins out there that can listen
for messages on this server and force the browser page to refresh, giving you a seamless what-you-seeis-what-you-get experience.
If you’re using the Spring Tool Suite, then you need only save
your code and you’ll see changes here. IntelliJ has no built-in
notion of a "save," and so no hook off of which to hang this event.
You’ll need to instead "build" the code. Go to Build > Build the
project. On a Mac, you could use Cmd + F9.
Web brings in everything you’d need today to build an application based on the Servlet specification
and traditional Spring MVC. It brings in form validation, JSON and XML marshalling, websocket
support, REST- and HTTP-controller support, and so much more. H2 is an embedded SQL database that
will lose its state on every restart. This is ideal for our first steps since we won’t have to install a
datbase (or reset it).
JDBC brings in support, like the JdbcTemplate, for working with SQL-based databases.
Lombok is a compile-time annotation processor that synthesizes things like getters, setters, toString()
methods, equals() methods, and so much more with but a few annotations. Most of these annotations
are self-explanatory in describing what they contribute to the class on which they’re placed.
Actuator contributes HTTP endpoints, under /actuator/\*, that give visibility into the state of the
application.
|