Spring Data Neo4j - @Autowired Repository == null [英] Spring Data Neo4j - @Autowired Repository == null

查看:37
本文介绍了Spring Data Neo4j - @Autowired Repository == null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Spring Data Neo4j 编写一个 web 应用程序.

I try to write a webapp using Spring Data Neo4j.

我有一个存储库:

public interface UserRepository extends GraphRepository<Neo4jUser> {} 

一个 applicationcontext.xml:

an applicationcontext.xml:

...
<context:component-scan base-package="de.wilke.test.neo4j" >
    <context:exclude-filter type="annotation" 
          expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- REST Connection to Neo4j server --> 
<bean id="restGraphDatabase" 
    class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
    <constructor-arg value="http://localhost:7474/db/data/" />
</bean>

<bean id="myservice" class="de.wilke.test.neo4j.Neo4jResource">
</bean>

<!-- Neo4j configuration (template) -->
<neo4j:config graphDatabaseService="restGraphDatabase" />

<!-- Package w/ automagic repositories -->
<neo4j:repositories base-package="de.wilke.test.repository" /> 

还有我的 Neo4jResource:

And my Neo4jResource:

@Controller
public class Neo4jResource {

    @Autowired
    public static UserRepository repo;
    ...

现在不能在控制器中使用 UserRepository 因为它是空的...

Now cannot use the UserRepository in the Controller because it is null...

错在哪里?

推荐答案

我能想到的几点:

  1. 您的<代码>不正确
  2. 您没有正确加载 applicationContext 并且 repo 没有正确实例化
  1. Your <neo4j:repositories base-package=".."/> is not correct
  2. You are not loading your applicationContext correctly and the repo does not get instantiated properly

首先检查您的包裹.另外,您的主应用程序中是否有类似以下内容?

Check your package first. Also, do you have anything like the following in your main application?

ConfigurableApplicationContext context;
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");

Neo4jResource myBean = context.getBean(Neo4jResource.class);
myBean.functionThatUsesTheRepo();

希望这会有所帮助...

Hope that this helps...

这篇关于Spring Data Neo4j - @Autowired Repository == null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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