Зависимость Spring не внедряется в HTTPSessionListener - PullRequest
0 голосов
/ 18 апреля 2019

Я хочу внедрить пружинную зависимость в свой пользовательский экземпляр HTTPSessionListener, следуя приведенной ниже ссылке. Если я удаляю запись из web.xml, метод sessionDestroyed не вызывается, однако, когда запись есть в методе web.xml, вызывается, но зависимостьXUSerMgr имеет значение null.

Как внедрить зависимости в HttpSessionListener, используя Spring? enter code here ring

@Component
public class RangerHttpSessionListener implements HttpSessionListener,ApplicationContextAware {

    @Autowired
    XUserMgr xUserMgr;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof WebApplicationContext) {
            ((WebApplicationContext) applicationContext).getServletContext().addListener(this);
        } else {
            //Either throw an exception or fail gracefully, up to you
            throw new RuntimeException("Must be inside a web application context");
        }
    }

    private static CopyOnWriteArrayList<HttpSession> listOfSession = new CopyOnWriteArrayList<HttpSession>();

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        listOfSession.add(event.getSession());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        if (!listOfSession.isEmpty()) {
            updateIsActiveStatusForAuthSession(event.getSession());
            listOfSession.remove(event.getSession());
        }
    }

    private void updateIsActiveStatusForAuthSession(HttpSession session) {
        xUserMgr.updateIsActiveStatusOfLoggedInUserWithHTTPSession(session.getId(),1);
    }

    public static CopyOnWriteArrayList<HttpSession> getActiveSessionOnServer() {
        return listOfSession;
    }

}

У меня есть следующая запись в моем файле web.xml.> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.apache.ranger.security.listener.RangerHttpSessionListener</listener-class> </listener>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...