spring 会话初始化错误

spring session initialization error

我正在按照本教程尝试实施 Spring 会话 Spring HttpSession Tutorial

但是我在启动应用程序时 运行 遇到初始化错误。

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionRepositoryFilter' defined in class org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration: Unsatisfied dependency expressed through constructor argument with index 1 of type [javax.servlet.ServletContext]: : No qualifying bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

我可能错了,但看起来没有注释的 ServletContext 实现,我应该添加 任何额外的依赖项 来解决这个问题吗?

致遇到此问题的任何人:
我切换到 spring 安全性的快照版本 1.1.0,问题消失了。
如果您使用的是 maven,请在 pom 中添加 spring 快照存储库。
This tutorial helped

虽然您可以使用 spring 模块版本并解决它,但由于 spring 版本不兼容,您可能会导致无数其他问题。 相反,您可以使用以下解决方案:ServletContextExposure 和 FactoryBean

我在使用 spring 3 和 XML 配置的遗留项目中使用了它:

 public class ServletContextExposure implements ServletContextAware, FactoryBean<ServletContext> {

    private ServletContext servletContext;

    @Override
    public ServletContext getObject() {
        return this.servletContext;
    }

    @Override
    public Class<?> getObjectType() {
        return ServletContext.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}

如果您使用 XML 配置,那么只需将其添加到您的 spring XML:

    <bean id= "servletContextExposure"  class="com.borderfree.boot.ServletContextExposure"/>