In my filter bean class, I added some beans dependency (with @Autowired annotation). But in the method doFilter(), all my dependency beans have nul
I was getting null pointer while accessing service class bean in filter class using Autowiring . I searched more than 100 links but not able to find solution. I am using spring Boot Application and configuration which is required to get bean in filter class :
FilterConfig.java which provides application context Object.
@Component
public class FilterConfig implements ApplicationContextAware{
private static ApplicationContext context;
public static ApplicationContext getApplicationContext() {
return context;
}
public static T getBean(String name,Class aClass){
return context.getBean(name,aClass);
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
context = ctx;
}
}
in Filter class , I used this like :
UserService userService =FilterConfig.getBean("UserService", UserService.class);
UserService this is bean name which is mentioned in
@Service("UserService")
public class UserServiceImpl implements UserService { ...}
No Configuration in main class of spring Boot :SpringBootServletInitializer