Make ServletContextListener spring aware

前端 未结 4 2049
鱼传尺愫
鱼传尺愫 2021-02-12 11:14

I am plugging in Spring to existing Java EE web Application. I have following lines in my web.xml:


    com.MyContextListen         


        
4条回答
  •  执笔经年
    2021-02-12 11:57

    What should I do to make MyContextListener be managed by Spring?

    It depends on which configuration way you are using. Anyway, you should tell directly Spring to use the class you have declared. That could be done by the following way:

    @WebListener
    public class MyContextListener implements ServletContextListener { ... }
    

    A class marked with this annotation (the Servlet 3.0 specification, 8.1.4) must implement one of these interfaces

    HttpSessionAttributeListener
    HttpSessionListener
    ServletContextAttributeListener
    ServletContextListener (+)
    ServletRequestAttributeListener
    ServletRequestListener
    HttpSessionIdListener
    

    that it actually does.

    Personally, I prefer a meta-annotation based approach which makes my configuration shorter and more concise.

    Spring should create all servlets and all web app infrastructure so everything happened in contextInitialized method of MyContextListener should be somehow handled by Spring.

    Yes, Spring will do it for you if you provide some information which could help it to register / configure / create / manage an instance.

    The information may be either meta-information (a template that tells how to create an instance, like BeanDefinitions) or a completed instance itself (usually, it gets passed programmatically that, in turn, leads to writing a huge amount of code).

    How can I achieve, by implementing some interface I suppose.

    You are implementing an interface to make your listener a listener (a class that describes specific methods which will be called at some points of time). Spring, itself, is responsible for guaranteeing such calls at those points of time, placing an object in the existing web infrastructure before.

提交回复
热议问题