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;
}
}
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
- Copy the lombok jar from the .m2 folder to the place where STS is installed
- Rename the jar as
lombok.jar - Find the
SpringToolSuite4.inifile (generally in the same folder) and add this line-javaagent:lombok.jarto the end of the file. - Restart STS
