2012-07-26 168 views
2

我嘗試使用Spring Data Neo4j編寫webapp。Spring Data Neo4j - @Autowired Repository == null

我有一個存儲庫:

public interface UserRepository extends GraphRepository<Neo4jUser> {} 

的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:

@Controller 
public class Neo4jResource { 

    @Autowired 
    public static UserRepository repo; 
    ... 

現在不能使用UserRepository在控制器,因爲它是空的...

錯誤在哪裏?

回答

0

@Autowired不適用於static字段。

從設計的角度來看,這個字段不是一個好主意static

+0

對不起,但其仍然是 「零」 ... – Tschakle 2012-07-26 11:18:09

0

一對夫婦的事情,我能想到的:

  1. <neo4j:repositories base-package=".."/> is not correct
  2. 你不加載你的applicationContext正確和repo沒有得到正確實例

首先檢查你的包。另外,你的主應用程序中是否有以下內容?

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

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

希望這有助於...