I have a service-client project which is in normal spring application , not spring boot .its used for mainly logging related things.which contains Interceptor , loggingservice impl class and some model classes for logging. I have added this module as a dependency to main application in pom.xml
.and i was able to inject and use the loggingService beans within the service layers of the main application.
Am getting NullPointerException
while auto-wiring loggingService within the interceptor .The bean is not available within the interceptor.but like i said it can be injected and used within the main application.
Also am not able to read properties using @Value
within the interceptor.
This is my Interceptor class .
@Component
public class LoggingInterceptor extends HandlerInterceptorAdapter {
@Autowired
LoggingService loggingService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
loggingService.info("Am in prehandle");
return true;
}
}
This is my configuration class where i register the interceptor with the main application
@Component
public class LoggingConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getLoginInterceptor());
}
@Bean
public LoggingInterceptor getLoginInterceptor() {
return new LoggingInterceptor();
}
}
My question is almost similar to this post Cannot Autowire Service in HandlerInterceptorAdapter , but its different like am referring the interceptor from another module , and like they suggested i tried to create the bean from the application. But the issues am facing right now is
- getting
NullPointerException
while injecting loggingService within interceptor, but its working in main application @Value
annotation also return null, not able to read from properties