Spring Data Neo4j “Hello, World” standalone app

主宰稳场 提交于 2019-12-25 03:43:39

问题


I'm trying to write a "Hello, World" with Spring Data Neo4j in a standalone app. It runs and actually creates the Neo4j database, but my @Autowired repo is not being initialized. I suspect the problem is in my main class, but I don't know what to try. Unsurprisingly, almost all the Spring tutorials I've found are about web apps.

What am I doing wrong?

config bean:

@Configuration
@EnableNeo4jRepositories(basePackages = "test2")
public class ConfigBean extends Neo4jConfiguration {
    private static final String DB_PATH = "/home/kevin/tmp/hello-spring-data-neo4j/";

    public ConfigBean() {
        setBasePackage("test2");
    }

    @Bean
    public GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
    }
}

node entity:

@NodeEntity
public class Foo {
    @GraphId
    private Long id;
}

repository:

public interface FooRepository extends GraphRepository<Foo> { }

main class:

@Component
public class Test2 {
    @Autowired
    FooRepository repo;

    public void doStuff() {
        System.out.println("repo: " + repo); // null!
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext("test2");
        new Test2().doStuff();
    }
}

It logs about 350 lines of output. These are the last few lines. I searched for this error message, but the impression I got is that it's unrelated to my problem.

20:44:30.630 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
20:44:30.631 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
20:44:30.635 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
repo: null

回答1:


Via the magical "ask a question, find the answer" effect, my main class now looks like this, and the repo is being assigned:

@Component
public class Test2 {
    @Autowired
    FooRepository repo;

    public void doStuff() {
        System.out.println("repo: " + repo);
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext("test2");
        Test2 test2 = context.getBean(Test2.class);
        test2.doStuff();
    }
}


来源:https://stackoverflow.com/questions/24817028/spring-data-neo4j-hello-world-standalone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!