Skip to content
Advertisement

Does Lombok’s @AllArgsConstructor Autowire the fields in spring boot automatically?

I am following a YouTube tutorial to build a spring boot application. The person used lombok and so he didn’t had the @Autowired annotation on any field of the class and his code works fine. However when I tried the same, the console shows the service is null.

Appropriate code and Output Screenshot attached for reference.

@AllArgsConstructor
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

private AppUserService appUserService;  
private BCryptPasswordEncoder bCryptpasswordEncoder;

    @Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(appUserService);
    provider.setPasswordEncoder(bCryptpasswordEncoder);
    return provider;
}

}

Console Output

On removing the @AllArgsConstructor, and using the @Autowired annotation my code works.

So, Does using lombok dependency with spring boot auto wires the fields automatically? if yes then what mistake am I doing?

PS : I am using Java 11 with Spring boot 2.4.5 on STS 4

Advertisement

Answer

As mentioned here its a problem of lombok with STS. To use lombok in STS, we need to do the following

  1. Copy the lombok jar from the .m2 folder to the place where STS is installed
  2. Rename the jar as lombok.jar
  3. Find the SpringToolSuite4.ini file (generally in the same folder) and add this line -javaagent:lombok.jar to the end of the file.
  4. Restart STS
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement