我正在尝试在独立应用程序中使用Spring Data Neo4j编写“Hello,World”。它运行并实际创建了Neo4j数据库,但我的@Autowired
repo没有被初始化。我怀疑问题出在我的班级,但我不知道该尝试什么。不出所料,我发现的几乎所有Spring教程都是关于网络应用程序的。
我做错了什么?
配置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行输出。这是最后几行。我搜索了这个错误信息,但我得到的印象是它与我的问题无关。
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
答案 0 :(得分:0)
通过魔法&#34;提出问题,找到答案&#34;效果,我的主类现在看起来像这样,并且正在分配回购:
@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();
}
}