📜 ⬆️ ⬇️

Spring Boot 2 and JDK 8: Are you still using @Param, @RequestParam and @PathVariable annotations? Then the article is for you.


Hello, Hablochitel!


While developing a Spring Boot 2 training project, I decided to experiment with @Param in Spring Data JPA queries, or rather the lack of them :


 @Transactional(readOnly = true) public interface UserRepository extends JpaRepository<User, Integer> { @Query("SELECT u FROM User u WHERE LOWER(u.email) = LOWER(:email)") Optional<User> findByEmailIgnoreCase(@Param("email") String email); List<User> findByLastNameContainingIgnoreCase(@Param("lastname") String lastName); } 

(about magic, how the second method works in the old publication In the footsteps of the Spring Pet Clinic ).


@Param removing @Param you can make sure that Spring works fine without them . I heard about the parameter in the compilation, which allows you not to duplicate the names in the annotations, but I did not do anything special, so I decided dig deeper podebazhit.


If you still use annotations from the article title, Spring Boot and JDK 8, please use the cat:




You can see that 2 strategies are used: StandardReflectionParameterNameDiscoverer and LocalVariableTableParameterNameDiscoverer . The first uses the JDK8 JEP 118: Access to Parameter Names at Runtime . According to SPR-9643 , if it fails to determine the names of the parameters by the first strategy, Spring tries to use the "ASM-based debug symbol analysis".




Yes, it is really included ... And what if I collect and launch a project through Maven?
The result is the same!



It means that:


  1. our reasoning is correct
  2. on the basis of the second ASM strategy, information could not be obtained (although I was running through Debug)

TOTAL: The -parameters flag in Spring Boot 2 is enabled by default, so if you inherit from spring-boot-starter-parent , then the parameter names are defined in @Param and @Param , @RequestParam , @PathVariable no longer required. Less code, less error.


For Spring Boot 1.x, the compilation flag can be included forcibly, see above.


PS: in the study used JDK 8, JDK 11 and Spring Boot 2.1.1
PSS: if you have information when the second strategy is LocalVariableTableParameterNameDiscoverer - plz share in the comments.


Thanks for attention!



Source: https://habr.com/ru/post/440214/