Spring Data Neo4j“您好,世界"独立应用 [英] Spring Data Neo4j "Hello, World" standalone app

查看:122
本文介绍了Spring Data Neo4j“您好,世界"独立应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在独立的应用程序中使用Spring Data Neo4j编写"Hello,World".它运行并实际上创建了Neo4j数据库,但是我的@Autowired存储库未初始化.我怀疑问题出在我的主要班上,但我不知道该怎么办.毫不奇怪,我发现的几乎所有Spring教程都是关于Web应用程序的.

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.

我在做什么错了?

config bean:

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);
    }
}

节点实体:

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

存储库:

public interface FooRepository extends GraphRepository<Foo> { }

主类:

@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();
    }
}

它记录约350行输出.这是最后几行.我搜索了此错误消息,但得到的印象是它与我的问题无关.

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

推荐答案

通过神奇的问问题,找到答案"的效果,我的主类现在看起来像这样,并且正在分配仓库:

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();
    }
}

这篇关于Spring Data Neo4j“您好,世界"独立应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆