Friedrich Ewald My Personal Website

Spring Boot Cheatsheet

TODO:

  • Extend JPARepository<Model, PrimaryKeyType> with custom functions
  • Find out about other parts of Spring

Spring Boot

Jakarta Validation

  1. @NotBlank: Checks that the annotated character sequence is not null and the trimmed length is greater than 0.
  2. @NotNull: Checks that the annotated value is not null, however it can be empty.
  3. @NotEmpty: Checks whether the annotated element is not null nor empty.

Model

  • Models need the lombok annotation @Getter and @Setter to be able to (de-)serialize correctly.

Service

  • Responsible for business logic
  • @Service annotation makes this a Bean whose lifecycle is managed by Spring

Data Access Object (DAO)

  • Responsible for data access layer
  • They access the Repository

Repository

  • Performs the actual database reads and writes
  • An interface that extends JpaRepository<JobApplication, Long>
  • Default methods are implemented
  • Based on Hibernate

Multithreading

  • Make thread safe via synchronized (this) { ... }
  • Methods can also be thread safe via synchronized
  • Semaphore usage via new Semaphore(int capacity)

ThreadPoolExecutor

public class MyRunner implements Runnable {
  @Override
  public void run() {
    // Actual task execution
  }
}

ExecutorService executor = Executors.newFixedThreadPool(5);
Runnable worker = new MyRunner();
executor.execute(worker);
executor.shutdown();
while (!executor.isTerminated()) {
  // wait...
}

Data Types

  • Basic types
    • String
    • double
    • int
    • float
  • (Double) Linked List List<Integer> l = new LinkedList<>();
    • l.add(1) O(1)
    • l.remove(int index) O(1)
  • Stack<Integer> s = new Stack<>();
    • s.push(1)
    • s.pop()
    • s.peek()
  • Heap PriorityQueue<Integer> q = new PriorityQueue<>();
    • q.offer(1);
    • q.peek();
    • q.poll();
  • Map Map<Integer, Integer> m = new HashMap<>();
    • m.size()
    • m.get(int key)
    • m.put(int k, int v)
  • Array int[] x = new int[5];
  • Deque Deque<Integer> d = new LinkedList<>();
    • d.addFirst(1)
    • d.getFirst()
    • d.addLast(1)
    • d.getLast()

Concurrent Types

Atomic types leverage CPU instructions to do “compare and swap” operations efficiently without using synchronized which would add a lot of overhead.

Available types are

  • AtomicInteger
  • AtomicBoolean
  • etc.