2014-03-18 51 views
0

看來的春:-boot,-core,數據的MongoDB,-data公地依賴

spring-core 
spring-data-commons 
spring-data-mongodb 

的GA版本,以及最近期發佈候選

spring-boot-starter-web 
spring-boot-autoconfigure 

不兼容。

這些工件的哪些版本相互兼容?

我有一個非常基本的Application.java,它將兩個對象放到MongoDB數據庫中,然後將它們取出並打印到屏幕上。

import game.acorn.entities.Entity; 
import game.acorn.mRepositoryInterfaces.EntityRepository; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class Application implements CommandLineRunner{ 

    @Autowired 
    private EntityRepository repository; 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    public void run(String... args) throws Exception { 

     repository.deleteAll(); 

     repository.save(new Entity("Jack", "Skellington")); 
     repository.save(new Entity("Blake", "Yarbrough")); 

     System.out.println("Entities found"); 
     for (Entity entity : repository.findAll()){ 
      System.out.println(entity); 
     } 

     System.out.println(); 
     System.out.println("Entity by first name 'Jack'"); 
     System.out.println(repository.findByFirstName("Jack")); 

     System.out.println(); 
     System.out.println("Entity by first name 'Blake'"); 
     System.out.println(repository.findByFirstName("Blake")); 
    } 


} 

當我運行

spring-core    version 4.0.2.RELEASE 
spring-data-commons  version 1.5.0.RELEASE 
spring-boot-starter-web version 1.0.0.RC4 
spring-data-mongodb  version 1.4.1.RELEASE 
spring-boot-autoconfigure version 1.0.0.RC3 

這個應用程序,我最終得到一個java.lang.NoClassDefFoundError

org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions 

它看起來像

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, 
     final BeanDefinitionRegistry registry) { 
    new RepositoryConfigurationDelegate(getConfigurationSource(), this.resourceLoader) 
      .registerRepositoriesIn(registry, getRepositoryConfigurationExtension()); 
} 

new RepositoryConfigurationDelegate(getConfigurationSource(), this.resourceLoader) 

是行58.看來,我使用的版本和spring-boot的依賴之間存在差異。

所以我嘗試了版本我spring-data-commonsversion 1.7.0.RELEASE

,然後類變爲可用,但然後我得到一個java.lang.NoSuchMethodError

org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport$1.<init>(AbstractRepositoryConfigurationSourceSupport.java:66) 

它看起來像

private AnnotationRepositoryConfigurationSource getConfigurationSource() { 
     StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(
       getConfiguration(), true); 
     return new AnnotationRepositoryConfigurationSource(metadata, getAnnotation(), 
       this.environment) { 
      @Override 
      public java.lang.Iterable<String> getBasePackages() { 
       return AbstractRepositoryConfigurationSourceSupport.this 
         .getBasePackages(); 
      }; 
     }; 
    } 

return new AnnotationRepositoryConfigurationSource(metadata, getAnnotation(), 
      this.environment) { 

是第65-66行。

當我檢查出

org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource 

我發現構造

public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation, 
      ResourceLoader resourceLoader, Environment environment) { 

     super(environment); 

     Assert.notNull(metadata); 
     Assert.notNull(annotation); 
     Assert.notNull(resourceLoader); 

     this.attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(annotation.getName())); 
     this.metadata = metadata; 
     this.resourceLoader = resourceLoader; 
    } 

希望多一個參數比AbstractRepositoryConfigurationSourceSupportspring-boot-autoconfigure給它。

所以,我的問題是,什麼樣的

spring-core 
spring-data-commons 
spring-boot-starter-web 
spring-data-mongodb 
spring-boot-autoconfigure 

版本相互兼容?

我的POM樣子

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.0.0.RC3</version> 
    </parent> 
    <groupId>Master-Project</groupId> 
    <artifactId>Master-Project</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <description>Master Project for MongoDB and Spring</description> 

    <modules> 
    </modules> 

    <dependencies> 

    <!-- Spring Stuff --> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>4.0.2.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
     <version>1.4.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-commons</artifactId> 
     <version>1.7.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <version>1.0.0.RC3</version> 
    </dependency> 



    <dependency> 
     <groupId>org.mongodb</groupId> 
     <artifactId>mongo-java-driver</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>3.1</version>  
    </dependency> 

    </dependencies> 
    <repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
    <!-- 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.3</source> 
      <target>1.1</target> 
     </configuration> 
     </plugin> 
    --> 
     <plugin> 
     <groupId>org.apache.maven.plugin</groupId> 
     <artifactId>maven-eclipse-plugin</artifactId> 
     <version>2.9</version> 
     <configuration> 
      <downloadSources>true</downloadSources> 
      <downloadJavadocs>true</downloadJavadocs> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

當我刪除的依賴性的版本標籤我得到相同這樣的方法並不例外。

+0

首先,您有內部版本不匹配。你爲什麼使用'starter-web'的1.0.0.RC4和'autoconfigure'的1.0.0.RC3?另外,請發佈您的項目POM;它看起來像你手動覆寫了幾個版本。 – chrylis

+0

+1他說的。您需要的唯一依賴包括spring-boot-starter- [web,data-mongodb]。如果您想要了解最新的Spring Data依賴關係,請使用快照版本(可能下週將有1.0版本,因此快照可能比RC4更接近最終版本)。 –

+0

Application類位於哪個包? – ChristopherZ

回答

2

如果您使用1.0.0.RC5或更高版本的Spring Boot依賴項pom(或初學者父項),這是固定的。