liveBook · Manning

Interface

As explained in Spring Start Here, Second Edition

A Java interface is a reference type that declares a responsibility or contract whose behavior must be provided by implementing classes.

When an object needs a job done, it can ask for the responsibility instead of naming a particular solution. A Java interface declares that responsibility as a contract: it describes what needs to happen, while an implementing class supplies how it happens. Multiple classes can implement the same interface differently. section 4.1 section 4.0

Sorting delivery details

A delivery-details printer needs sorted packages, but it need not depend directly on a sorter by address. It can depend on a Sorter interface instead. A different implementation can provide a new sorting order without requiring the printer to depend on that implementation directly. section 4.1.1

Figure 4.3 Using an interface to decouple the responsibilities. Instead of depending directly on an implementation, the DeliveryDetailsPrinter object depends on an interface (a contract). DeliveryDetailsPrinter can use any object implementing this interface instead of being stuck to a specific implementation.

The printer depends on a sorting contract rather than a particular sorter. section 4.1.1

For a comment-publishing service, a CommentRepository interface can declare the storage operation. The declaration names the operation and its input without specifying how storage works: section 4.1.3

Listing 4.2 Defining the CommentRepository interface
public interface CommentRepository {
 
  void storeComment(Comment comment);
}

The interface declares storeComment for a Comment. section 4.1.3

DBCommentRepository implements that contract and defines storeComment. In this small example, its method prints a message rather than storing anything in a database. CommentService accepts the repository and a notification interface through its constructor, then delegates to both when publishing a comment. section 4.1.3

Listing 4.3 Implementing the CommentRepository interface
public class DBCommentRepository implements CommentRepository {
 
  @Override
  public void storeComment(Comment comment) {
    System.out.println("Storing comment: " + comment.getText());
  }
}

The implements keyword connects the class to the contract, and the method supplies the behavior. section 4.1.3

In a Spring application, dependency injection can supply a bean that implements the requested interface. Put stereotype annotations on classes Spring should create as beans, not on the interface. If multiple beans implement the interface, use @Primary to designate a default or @Qualifier to select one by name. section 4.5

Sometimes a framework supplies the implementation. With Spring Cloud OpenFeign, an annotated client interface declares REST calls, and OpenFeign provides an injectable implementation. Spring Data likewise provides implementations of declared repository interfaces at runtime. section 11.3 section 14.0

Listing 11.4 Declaring an OpenFeign client interface
@FeignClient(name = "payments",    #A
             url = "${name.service.url}")
public interface PaymentsProxy {
 
  @PostMapping("/payment")    #B
  Payment createPayment(
      @RequestHeader String requestId,   #C
      @RequestBody Payment payment);     #C
 
}

This OpenFeign interface declares a payment request using annotations for the endpoint, header, and body. section 11.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines contracts and works through interface declarations, implementing classes, constructor injection, and Spring bean selection.
Examples
Uses delivery sorting, comment publishing, and a payments endpoint client to illustrate interface contracts.
What you can do after
Apply interfaces in Java and Spring to separate responsibilities and select implementations for injection.
How it connects
Connects interfaces with dependency injection, Spring beans, repositories, and REST clients.

Also explained in

Compare how 13 books explain interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.