Posts Tagged - spring

Spring in Action (1/5) - Foundational Spring

(This are my notes taken from the book Spring in Action 5th Edition)

Spring parts

  • Spring core Provides the core container, dependency injection framework, Spring MVC (Rest APIs), Spring’s web framework and support for template-based JDBC and reactive programming with Spring WebFlux.
  • Spring boot Starter dependencies and autoconfiguration.
  • Spring data Provides the ability to define your application’s data repository as Java interfaces. Works with relational (JPA), document (Mongo) and graph (Neo4j) databases.
  • Spring security Authentication, authorization and API security.
  • Spring integration & Batch helps integrate with other applications
  • Spring cloud helps with microservices

Read More

Spring CORS

CORS (Cross-Origin Resource Sharing)

It’s a mechanism to let a web application running at one domain, protocol or port have permission to access resources from a server at a different one.

This is needed if you have, for example, a Frontend running on port :3000 (React) consuming a Backend API running on port :34831 (custom port for Spring). Unless CORS are set, FE will not be able to access BE resources.

In Spring

It’s possible to enable them for a single RestResource or globally for the whole application.

(This example has been done in Kotlin)

By RestResource

class CourseRestResource {
  // dependencies and methods

Read More


Thymeleaf is an HTML template engine, which provides full Spring support.

<h3>Designate your wrap:<h3>
  <div th:each="ingredient: ${wrap}">
    <input name="ingredients" type="checkbox"
            th:value="${}" />
    <span th:text="${}">ING</span>


  • @{} produces a context-relative path to the /static/ folder

Simple Tags

  • th:src="@{images/taco.png}" retrieves an image with relative path from /static/
  • th:href="@{/styles.css}" retrieve a css file

Read More

Spring Beans

A spring bean is the basic building block of a Spring App. In its basis, it’s an Object which Spring Framework manages at runtime.

This management includes:

  • Creating an Object
  • Filling dependencies
  • Intercepting method calls
  • Destroying the Object

Define a Spring Bean

There’re 3 ways:

  • declare it with @Component annotation
  • @Bean annotation at a Bean Factory
  • .xml config. file (old way)

Read More

Spring Cache

Spring Cache

A cache itself may be imagined as a key-value map. For a basic Cache we need:

  • @EnableCaching tag in @Configuration class
  • Declare a CacheManager Bean
  • Tag the method to cache w. @Cacheable
  • Create a method with @CacheEvict

We may declare +1 cache(s) at the cache manager and select the one we want to use in the method we tag. As key for the cache we may use any conjunction of the parameters given to the method, or if this is a class, any of it’s accessible variables. The cache will only be triggered when the exact key is given again. Then the method won’t be executed and the value will be directly given from the cache. If the parameters don’t match any key, the method will be executed as normal and then the value will be saved into the cache to be returned the next time.

Caution with logs in big apps as they need to be written accordingly.
The hard part is not knowing when to cache something, but to know when to Evict the cache.

Read More