2

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

Why So Serious
  • 129
  • 1
  • 9
  • You don't need autowired, just constructor injection – Yassin Hajaj Apr 23 '21 at 20:41
  • i know, but thats exactly the problem lombok should provide the constructor automatically – Why So Serious Apr 23 '21 at 20:53
  • 1
    Since Spring 4.3, it is no longer needed to use `@Autowired` if there is only 1 constructor. See https://stackoverflow.com/questions/41092751/spring-injects-dependencies-in-constructor-without-autowired-annotation – Wim Deblauwe Apr 23 '21 at 22:24

1 Answers1

1

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
Why So Serious
  • 129
  • 1
  • 9