问题
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:
- Your
<neo4j:repositories base-package=".."/> is not correct
- You are not loading your
applicationContext
correctly and therepo
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