I have my JWT utils class:
@Component
public class JwtUtils {
private final String jwtSecret;
private final int jwtExpirationMs;
public JwtUtils(@Value("${app.jwtSecret}") String jwtSecret, @Value("${app.jwtExpirationMs}") String jwtExpirationMs)){
this.jwtSecret = jwtSecret;
this.jwtExpirationMs = jwtExpirationMs;
}
...
}
and inside my WebTokenConfig I need to initialize it for my AuthTokenFilter:
@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter(new JwtUtils(..., ...), ...);
}
So basically, JwtUtils needs 2 parameters, but those parameters should be set from properties' context. How to handle the constructor's injection?
Plus, I'm trying to test JwtUtils class with a junit test with mockito. Using @Autowired on fields (and not on constructor) and then instantiating the bean with new JwtUtils(), those parameters are not being init'd, staying null and 0 (even with @TestPropertySource, System.setProperties, ...). That's why I'm trying with constructor injection (which as I read is always the best option). This way I just have to pass the parameters through the new JwtUtils() and that's it.
But when done inside other beans (no test), it just makes no sense to me to pass them since they should be retrieved from context and not be passed. I'm probably missing something.