neo4j:找不到依赖的类型的限定bean

时间:2016-03-21 22:04:06

标签: java spring maven neo4j

我试图运行这个例子: https://spring.io/guides/gs/accessing-data-neo4j/ 我使用Maven作为构建工具。 我收到如下错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run (default-cli) on project gs-accessing-data-neo4j: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'demo' defined in hello.Application: Unsatisfied dependency expressed through constructor argument with index 0 of type [hello.PersonRepository]: No qualifying bean of type [hello.PersonRepository] 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 [hello.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} -> [Help 1]

有什么问题?

这是我的PersonRepository.java文件:

package hello;

import java.util.List;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;

import org.springframework.data.repository.CrudRepository;

@EnableNeo4jRepositories(basePackageClasses=CrudRepository.class)
public interface PersonRepository extends CrudRepository<Person, String>
{

    Person findByName(String name);

    List<Person> findByTeammatesName(String name);

}

1 个答案:

答案 0 :(得分:1)

如果您仔细阅读本教程,您会看到:

  

在配置中,您需要添加@EnableNeo4jRepositories注释以及扩展Neo4jConfiguration类以方便地启动所需的组件。

     

默认情况下,@EnableNeo4jRepositories将扫描当前包以查找扩展Spring Data存储库接口之一的任何接口。使用它basePackageClasses=MyRepository.class可以安全地告诉Spring Data GemFire按类型扫描不同的根包,如果您的项目布局有多个项目而且找不到您的存储库。

     

缺少的一件是图数据库服务bean。在这种情况下,您使用的是EmbeddedGraphDatabase,它会在accessingdataneo4j.db创建并重新使用基于文件的数据存储。

因此,您需要编写一个将添加Sprint Boot应用程序配置的类。我们称之为ApplicationConfig.java。将教程所说的内容放入代码中,我们提出:

package hello;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;

@Configuration
@EnableNeo4jRepositories
class ApplicationConfig extends Neo4jConfiguration {

    public ApplicationConfig() {
        setBasePackage("hello");
    }

    @Bean
    GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db");
    }
}

如果将此类添加到hello包中,则应用程序将正确运行。

请注意,您应该保留教程中定义的PersonRepository,如下所示:

package hello;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface PersonRepository extends CrudRepository<Person, String> {

    Person findByName(String name);

    List<Person> findByTeammatesName(String name);

}

作为旁注,您可以在Spring GitHub repo上看到完整的代码。