1

My SessionListener component looks like this:

@Component
public class SessionListener implements HttpSessionListener{

    @Autowired
    private PeerConnectionRepository peerConnectionRepository;

    ...
}

Cause I use @Autowired i have to load SessionListener as component. I try to do it like this:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Autowired
    SessionListener sessionListener;

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(sessionListener);
    }

   ...
}

But I get Failed to start component error. How to add this SessionListener as component to servletContext? Please for help, cheers.

K4liber
  • 785
  • 1
  • 8
  • 17
  • Look at this answer: http://stackoverflow.com/questions/37503372/java-how-use-spring-autowired-in-systeminitializer-class/37504322#37504322 – alex Sep 15 '16 at 09:49
  • How can I addListener to ServletContext after load application? – K4liber Sep 15 '16 at 10:56
  • Yes in this Answer you will find, that this is not possible. there is no `@Autowired` in WebAppInitializer – alex Sep 15 '16 at 11:03
  • So I cannot make changes in ServletContext after load application and cannot autowire component to change ServletContext before load application? – K4liber Sep 15 '16 at 11:09
  • yes, right. unfortunately you can't – alex Sep 15 '16 at 11:15
  • Any idea how to do something on sessionDestroyed in another object? I try to add WebListener annotation, and then it automatically add listener (i didnt have to use addListener method with new instance of SessionListener) but this annotation added SessionListener by constructor not like component, because peerConnectionRepository is still null. – K4liber Sep 15 '16 at 11:22
  • I just get this bean from ApplicationContext and it works, thanks! http://stackoverflow.com/questions/13836310/call-a-service-layer-function-in-listener-class-on-session-destroyed-in-spring – K4liber Sep 15 '16 at 11:30

2 Answers2

0

I think you are creating two instances of the same bean because you have probably somewhere component scan(I had the same problem and realized that @SpringBootApplication contains also component scan) in my case the problem was solved just with declaring it as normal class and then creating bean of it. I hope that also works for you.

Tano
  • 1,285
  • 1
  • 18
  • 38
  • Yes, but after declaring it as normal class i cannot user in this class annotation Autowired, which I need to do some thing with repository on event SessionDestroyed. – K4liber Sep 15 '16 at 09:24
  • Declare it as normal class, then in some Configuration class and there declare a new method which returns only a new instance of this "normal" class and put an annotation @Bean for this method and after that you can easily autowire – Tano Sep 15 '16 at 09:46
  • But i cant use Autowire in WebAppInitializer because of not loaded components yet – K4liber Sep 15 '16 at 10:41
0

I'm not sure if you can autowire a field to WebApplicationInitializer. Initializer is just an object configuring DispatcherServlet and creating Application Context. It is not a bean. You can try this: https://www.mkyong.com/spring/spring-how-to-do-dependency-injection-in-your-session-listener/ and get beans via WebApplicationContextUtils. But it doesn't look like best practice to me.

Yuri Plevako
  • 311
  • 1
  • 6