Академический Документы
Профессиональный Документы
Культура Документы
The Spring Framework is the most widely used framework for enterprise Java
applications. It’s a complete platform with many Spring subprojects under the
“Spring umbrella” that help you with almost anything you need. It has a great
community, and it’s evolving much faster than Java EE (and has been since the
beginning actually).
In this book, I’m not going to explain much about the Spring Framework theory.
The goal here is to show you how to use it in practice. In Part 3, “Code by Feature,”
I’ll explain the code snippets step-by-step using the Spring Framework. I believe
that this will be enough for most readers. If you still have trouble understanding this
section, I suggest you study up on Spring before moving on. Spring has great
documentation,1 including many practical examples and tutorials.
1
https://spring.io/docs
8.1 Spring Boot
Even better, a Spring subproject called Spring Boot2 comes in handy when
dealing with configurations and bootstrapping projects. It takes the philosophy
of convention over configuration quite seriously, so when bootstrapping your
project with Spring Boot, minimum configuration is required because the
defaults will satisfy your needs most of the time. Spring Boot even comes with
embedded servlet containers such as Tomcat, Jetty, or Undertow. Basically,
it infers from your project’s dependencies what you are going to use and
automatically configures it for you.
To get your enterprise application running, you just have to start a public
static void main method. To start a Spring Boot application from the
command line, type java -jar my_application.jar and you’re done. That was
exactly what you did when running the chat app locally. In my opinion, Spring
Boot is one of the most amazing subprojects of Spring. (Or would it be Spring
Data? Or Spring MVC? No! It’s Spring Security! What about Spring WebSocket?
Oh, I don’t know…well, forget about it, and let’s move on!)
Make sure to take a look at the list of Spring subprojects3 available. It’s quite
impressive, isn’t it? Indeed, it’s possible to integrate Spring with everything.
2
https://projects.spring.io/spring-boot/
3
https://spring.io/docs/reference
48
Chapter 8 ■ The Spring Framework
spring:
datasource:
url: jdbc:mysql://localhost:3306/ebook_chat
username: root
password: root
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: true
hibernate:
ddl-auto: validate
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
public interface UserRepository extends JpaRepository<User, String> {
49
Chapter 8 ■ The Spring Framework
But what if, for example, you need to find a user by e-mail address? Well, you
could simply declare a method signature respecting the Spring Data pattern,4
and you’re done.
4
https://docs.spring.io/spring-data/jpa/docs/current/reference/
html/#repositories.query-methods.query-creation
50
Chapter 8 ■ The Spring Framework
The whole idea here is that you declare a method and Spring Data
dynamically implements it for you.
If you need a customized query, just declare your method, annotate it with
@Query5 providing the custom JPQL6 query, and use it.
Of course, for JPA repositories work, the User class must be annotated with
JPA8 annotations. Here’s an example:
@Entity
@Table(name = "user")
public class User {
@Id
private String username;
private String password;
private String name;
private String email;
...
}
5
https://docs.spring.io/spring-data/jpa/docs/current/reference/
html/#jpa.query-methods.at-query
6
http://docs.oracle.com/html/E13946_04/ejb3_langref.html
7
https://docs.spring.io/spring-data/jpa/docs/current/reference/
html/#_native_queries
8
www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.html
51
Chapter 8 ■ The Spring Framework
The method signature patterns work the same as explained for JPA.
What actually changes is that the model should be annotated with Spring Data
Cassandra annotations instead of JPA.
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
@Table("messages")
public class InstantMessage {
@PrimaryKeyColumn(name = "username", ordinal = 0,
type = PrimaryKeyType.PARTITIONED)
private String username;
52
Chapter 8 ■ The Spring Framework
The same goes for Spring Data Redis repositories. As you will see in Part 3,
“Code by Feature,” Redis is being used in the chat app to manage the chat rooms
and the connected users. Take a look at how the model and the repository look:
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
@RedisHash("chatrooms")
public class ChatRoom {
@Id
private String id;
private String name;
private String description;
private List<ChatRoomUser> connectedUsers = new ArrayList<>();
...
}
Spring Data repositories really make your life easier, but sometimes you may
need extra features that they don’t provide. For instance, for Spring Data Redis
repositories, the technology maps your models only to Redis hashes, but as you’ve
learned, there are many other data structures available in Redis. In that case, you’d
have to use the Spring Data templates (CassandraTemplate, RedisTemplate, and so
on), which are straightforward to use as well.
53