Spring MVC + Thymeleaf: adding variable to all templates' context

If you simply want something from your application.properties into your thymeleaf template, then you can make use of Spring's SpEL.

${@environment.getProperty('name.of.the.property')}

@ControllerAdvice work for me:

@ControllerAdvice(annotations = RestController.class)
public class AnnotationAdvice {

     @Autowired
     UserServiceImpl userService;

     @ModelAttribute("currentUser")
     public User getCurrentUser() {
         UserDetails userDetails = (UserDetails) 
         SecurityContextHolder.getContext()
               .getAuthentication().getPrincipal();

      return userService.findUserByEmail(userDetails.getUsername());
     }
  }

Several ways to do this.

If you want to add a variable to all views served by a single controller, you can add a @ModelAttribute annotated method - see reference doc.

Note that you can also, using the same @ModelAttribute mechanism, address multiple Controllers at once. For that, you can implement that @ModelAttribute method in a class annotated with @ControllerAdvice - see reference doc.