Spring Data Neo4j - @Autowired Repository == null

試著忘記壹切 提交于 2019-12-13 15:24:01

问题


I try to write a webapp using Spring Data Neo4j.

I have an Repository:

public interface UserRepository extends GraphRepository<Neo4jUser> {} 

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" /> 

And my Neo4jResource:

@Controller
public class Neo4jResource {

    @Autowired
    public static UserRepository repo;
    ...

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

Where is the mistake?


回答1:


@Autowired doesn't work for static fields.

Also it's not a good idea from design point of view to make this field static.




回答2:


A couple of things that I can think of:

  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...



来源:https://stackoverflow.com/questions/11665670/spring-data-neo4j-autowired-repository-null

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