2016-04-09 76 views
0

我讀了幾個類似的問題和答案autowireBeanCreationException和看起來問題的主要來源通常在ComponentScan註釋和項目樹。春季啓動,Autowire Mongo回購生成BeanCreationException

但我仍然不明白爲什麼我的應用程序拋出此異常。 這裏是

<?xml version="1.0" encoding="UTF-8"?> 
<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> 

    <groupId>com.test.game</groupId> 
    <artifactId>gameP</artifactId> 
    <version>0.1.0</version> 
    <packaging>jar</packaging> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.3.RELEASE</version> 
    </parent> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>2.0.2-beta</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-mongodb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-lang3</artifactId> 
      <version>3.0</version> 
     </dependency> 
    </dependencies> 
    <properties> 
     <java.version>1.8</java.version> 
    </properties> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

SpringBootApplication註釋

package core.main.exec; 

@SpringBootApplication 
@Configuration 
@ComponentScan("core.main") 
public class ApplicationRunner implements CommandLineRunner { 
    @Autowired 
    GameEntityRepository repo; 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(ApplicationRunner.class, args); 
    } 

    @Override 
    public void run(String... args) throws Exception { 
     Game newGame = new Game(); 
     newGame.setId(1213123L); 
     newGame.setName("halo 123"); 
     newGame.setDeck("Duck"); 
     newGame.setMedium_url("url"); 
     repo.save(newGame); 
    } 
} 

和知識庫的應用亞軍被定義爲波紋管

package core.main.controller; 

public interface GameEntityRepository extends MongoRepository { 
} 

,並命名爲Game蒙戈實體是pom.xml中波紋管

package core.main.mongoentity; 

@Document 
public class Game { 
    @Id 
    private Long id; 
    private String deck; 
    private String name; 
    private String medium_url; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getDeck() { 
     return deck; 
    } 

    public void setDeck(String deck) { 
     this.deck = deck; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getMedium_url() { 
     return medium_url; 
    } 

    public void setMedium_url(String medium_url) { 
     this.medium_url = medium_url; 
    } 
} 

當我運行該應用程序我得到一個BeanCreationException與以下錯誤消息

無法自動裝配領域:core.main.controller.GameEntityRepository core.main.exec.ApplicationRunner.repo

回答

0

我認爲包掃描

重構這個

@SpringBootApplication 
@Configuration 
@ComponentScan("core.main") 
public class ApplicationRunner implements CommandLineRunner {} 
0導致該錯誤

這樣

@SpringBootApplication 
@EntityScan({"core.main.mongoentity"}) 
@EnableJpaRepositories(basePackages = {"core.main.controller"}) 
public class ApplicationRunner implements CommandLineRunner {} 

,也不要使用@ComponentScan("core.main"),因爲@SpringBootApplication提供了不正確你不聲明實體MongoRepository

@EnableAutoConfiguration屬性和@ComponentScan

,也是你的資料庫重構你的資料庫

public interface GameEntityRepository extends MongoRepository {} 

這樣

public interface GameEntityRepository extends MongoRepository<Game,Long>{} 

,並在pom.xml中

 <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
     </dependency> 
+0

添加這種依賴關係,我不知道爲什麼你添加的註釋@EnableJpaRepositories。但只是添加實體掃描會給我一個新的錯誤,說錯誤創建名爲'embeddedServletContainerCustomizerBeanPostProcessor'的bean。 – Salman9

+0

這個tomcat錯誤你現在你的bean創建成功 –

+0

在pom中沒有tomcat依賴。也許我錯過了那裏的東西?! – Salman9