【问题标题】:Spring Data MongoDB not working on Spring 3.0Spring Data MongoDB 不适用于 Spring 3.0
【发布时间】:2016-07-18 04:41:59
【问题描述】:

我正在创建一个Spring User login 接口,我需要将用户详细信息存储在mongodb 数据库中。

我正在使用 Spring 3.0,但在我的项目中无法使用 Spring-data-mongodb,因为我在某处读到它需要更高版本的 Spring。

如何使Spring-mongo-db 在我当前的项目中工作,或者如何正确更新我的pom.xml 中的依赖项?

这是我目前的pom.xml

<modelVersion>4.0.0</modelVersion>
<groupId>controller</groupId>
<artifactId>spring-security-ldap-embedded</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>spring-security-ldap-embedded Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
    <version>3.0.5.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-all</artifactId>
    <version>1.5.5</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.5.6</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.5.6</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-mongodb</artifactId>
   <version>1.8.4.RELEASE</version>
   <type>jar</type>
   <exclusions>
     <exclusion>
       <groupId>org.slf4j</groupId>
       <artifactId>jcl-over-slf4j</artifactId>
     </exclusion>
   </exclusions>
</dependency>

<dependency>
   <groupId>org.mongodb</groupId>
   <artifactId>mongo-java-driver</artifactId>
   <version>3.2.2</version>
   <type>jar</type>
</dependency>

</dependencies>

<build>
<finalName>spring-security-ldap-embedded</finalName>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>
</plugins>    
</build>

这是当前的 mongo-java-driver 和其他 mongo 依赖项:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>1.8.4.RELEASE</version>
  <type>jar</type>
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>3.2.2</version>
  <type>jar</type>
</dependency>

我的 securityconfig.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-4.1.xsd">

<!-- This is where we configure Spring-Security  -->

<security:http auto-config="true" use-expressions="true" >

        <security:intercept-url pattern="/login" access="permitAll"/>
        <security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/>
        <security:intercept-url pattern="/common" access="hasRole('ROLE_USER')"/>

        <security:form-login
            login-page="/login"
            authentication-failure-url="/login?error=true"
            default-target-url="/main/common"/>

        <security:logout 
            invalidate-session="true"
            logout-success-url="/login"
            logout-url="/logout"/>

</security:http>

<!-- 
        For authentication:
        user-search-filter: the attribute name that contains the user name 
    user-search-base: the base path where to find user information

    For authorization:
    group-search-filter: the attribute name that contains the full dn of a user
    group-search-base: the base path where to find role information
    group-role-attribute: the attribute name that contains the role type
    role-prefix: the prefix to be added when retrieving role values

        For server access:
    manager-dn: the full dn of the person that has access to an LDAP server
    manager-password: the password of the person that has access to an LDAP server
-->

<security:authentication-manager>
        <security:ldap-authentication-provider  
                user-search-filter="(uid={0})"
                user-search-base="ou=users"
                group-search-filter="(uniqueMember={0})"
                group-search-base="ou=groups"
                group-role-attribute="cn"
                role-prefix="ROLE_">
        </security:ldap-authentication-provider>
</security:authentication-manager>

<!-- 
        Use an embedded LDAP server. We need to declare the location of the LDIF file
        We also need to customize the root attribute default is "dc=springframework,dc=org"
    -->

<security:ldap-server ldif="classpath:mojo.ldif"  root="o=mojo"/>

我的 applicationcontext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
        For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="controller" />

<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
<mvc:annotation-driven /> 

【问题讨论】:

    标签: java spring mongodb maven spring-mvc


    【解决方案1】:

    让它工作的唯一方法是升级你的 spring 版本。

    Spring Data MongoDB 1.x 二进制文件需要 JDK 级别 6.0 及以上,以及 Spring Framework 4.2.6.RELEASE 及以上。

    http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#requirements


    编辑

    • 完全删除现有的 .m2/repository 或与 spring 和 mongo 相关的 jar 和文件夹
    • 修改你的 pom.xml 并指定合适的 spring 版本。
    • 执行 mvn 全新安装

    希望它能解决你的问题。

    【讨论】:

    • 但我尝试升级我的 pom.xml,但项目停止部署。我应该在我的项目中做哪些其他更改?我应该如何正确更新我的其他文件?
    • 尝试我在答案的编辑部分添加的更改
    • 实际上我正在使用一个 IDE (Netbeans),在其中我修改了 pom.xml 并用它们的最新版本替换了所有旧版本(用于 spring-security 的 4.3 和 4.1),但我还有一些其他的securityconfig.xmlapplicationcontext.xml 文件,需要修改,怎么做?
    • 您可以使用任何 IDE,但您仍然可以执行我在编辑部分中提到的步骤。转到您拥有代码库的相应路径/位置(例如 D:/myworkspace/myproject),在该位置打开命令提示符或终端并执行编辑部分中列出的所有步骤,然后在 IDE 中更新依赖关系。
    猜你喜欢
    • 2019-03-25
    • 2023-03-06
    • 2017-10-25
    • 1970-01-01
    • 2016-09-16
    • 2020-07-04
    • 2016-12-03
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多