使用弹簧3.1.2弹簧会话不工作

时间:2016-10-15 14:45:18

标签: spring mongodb spring-3 spring-session

我想在当前的春季3.1.2项目中使用spring session mongo,但失败了。 我的POM依赖是这样的:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.0.M2</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-mongo</artifactId>
            <version>1.2.2.RELEASE</version>
            <type>pom</type>
        </dependency>

我的配置是:

@Configuration
@EnableMongoHttpSession
public class HttpSessionConfig extends AbstractMongoConfiguration {

    @Value("${mongo-url}")
    String mongo_url;

    @Bean
    public JdkMongoSessionConverter jdkMongoSessionConverter() {
            return new JdkMongoSessionConverter(); 
    }


    @Bean
    public Mongo mongo() throws UnknownHostException {
        String url = mongo_url;
        ArrayList<ServerAddress> addr = new ArrayList<ServerAddress>();
        for (String s : url.split(",")) {
            addr.add(new ServerAddress(s));
        }
        Mongo mongo = new Mongo(addr);
        return mongo;
    }

    public @Bean MongoTemplate mongoTemplate() throws UnknownHostException {
        return new MongoTemplate(mongo(), "mydb");
    }



    @Override
    protected String getDatabaseName() {
        return "mydb";
    }
}

问题出在我开始使用以下项目之后:

mvn tomcat7:run,

我得到这样的错误:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.security.saml.web.HttpSessionConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:346)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)

春季会议是否仅适用于spring4及以上版本?我该如何解决这个问题?

由于

更新: 我将版本更改为3.2.14.RELEASE:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.14.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.14.RELEASE</version>
            <scope>compile</scope>
        </dependency>
         <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-mongo</artifactId>
                <version>1.2.2.RELEASE</version>
                <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
            <version>1.2.2.RELEASE</version>    
        </dependency>

这个配置:

@Configuration
@EnableMongoHttpSession
public class HttpSessionConfig extends AbstractMongoConfiguration {

    //@Value("${mongo-url}")
    String mongo_url;

    @Bean
    public JdkMongoSessionConverter jdkMongoSessionConverter() {
            return new JdkMongoSessionConverter(); 
    }


    @Bean
    public Mongo mongo() throws UnknownHostException {
        mongo_url = "127.0.0.1:27017";// "dev-ngcsc:27017,dev1-ngcsc:27017,dev2-ngcsc:27017";
        String url = mongo_url;
        ArrayList<ServerAddress> addr = new ArrayList<ServerAddress>();
        for (String s : url.split(",")) {
            addr.add(new ServerAddress(s));
        }
        Mongo mongo = new Mongo(addr);
        return mongo;
    }

    public @Bean MongoTemplate mongoTemplate() throws UnknownHostException {
        return new MongoTemplate(mongo(), "ngcsc");
    }



    @Override
    protected String getDatabaseName() {
        return "ngcsc";
    }
}

现在我收到了这个错误:

Oct 15, 2016 8:15:55 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoSessionRepository' defined in class org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.mongodb.core.MongoOperations]: : Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/security/saml/web/HttpSessionConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.mongodb.core.MongoTemplate org.springframework.security.saml.web.HttpSessionConfig.mongoTemplate() throws java.net.UnknownHostException] threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/security/saml/web/HttpSessionConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.mongodb.core.MongoTemplate org.springframework.security.saml.web.HttpSessionConfig.mongoTemplate() throws java.net.UnknownHostException] threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd

看来mongo问题,我有什么想法可以改进吗?

我已将版本升级到4,但另一个问题是:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionRepositoryFilter' defined in class path resource [org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.session.SessionRepository]: : No qualifying bean of type [org.springframework.session.SessionRepository] 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 [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

我是否错过了任何bean定义?

1 个答案:

答案 0 :(得分:1)

从文档中引用

  

如果您正在使用其他Spring库(不是必需的),那么最低限度   所需的版本是Spring 3.2.14。我们重新运行所有单元测试   针对Spring 3.2.x,我们建议使用最新的Spring 4.x版本   如果可能的话。

请参阅spring-session documentation