Can't integrate Spring Boot Data with Neo4j -
i started spring boot data neo4j , trying finish movie tutorial. got error not sure how debug. error summary : (1) error creating bean (2)could not autowire field. help, thank you.
my files structure follows :
springbootapplication.java
package com.test.springdataneothree; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.commandlinerunner; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.boot.autoconfigure.springbootapplication; import com.test.services.movieservice; @enableautoconfiguration @springbootapplication public class springdataneothreeapplication implements commandlinerunner { @autowired movieservice movieservice; public static void main(string[] args) { springapplication.run(springdataneothreeapplication.class, args); } @override public void run(string... arg0) throws exception { system.out.println("main spring boot class"); movieservice.countmovie(); } }
movie.java
package com.test.movie; import org.neo4j.ogm.annotation.graphid; import org.neo4j.ogm.annotation.nodeentity; @nodeentity(label ="movie") public class movie { @graphid private long id; string title; int released; string tagline; public movie() { } public long getid() { return id; } public void setid(long id) { this.id = id; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public int getreleased() { return released; } public void setreleased(int released) { this.released = released; } public string gettagline() { return tagline; } public void settagline(string tagline) { this.tagline = tagline; } }
movieservice.java
package com.test.services; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.test.repositories.movierepository; @service("movieservice") public class movieservice { @autowired movierepository movierepository; public void countmovie() { movierepository.count(); } }
movierepository.java
package com.test.repositories; import java.util.collection; import java.util.list; import java.util.map; import org.springframework.data.neo4j.annotation.query; import org.springframework.data.neo4j.repository.graphrepository; import org.springframework.data.repository.query.param; import org.springframework.stereotype.repository; import com.test.movie.movie; @repository public interface movierepository extends graphrepository<movie> { movie findbyid(long id); movie findbytitle(@param("title") string title); @query("match (m:movie) m.title =~ ('(?i).*'+{title}+'.*') return m") collection<movie> findbytitlecontaining(@param("title") string title); @query("match (m:movie)<-[:acted_in]-(a:person) return m.title movie, collect(a.name) cast limit {limit}") list<map<string,object>> graph(@param("limit") int limit); }
myneo4jconfiguration.java
package com.test.configuration; import org.neo4j.ogm.session.sessionfactory; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.data.neo4j.config.neo4jconfiguration; import org.springframework.data.neo4j.repository.config.enableneo4jrepositories; import org.springframework.scheduling.annotation.enablescheduling; import org.springframework.transaction.annotation.enabletransactionmanagement; @enabletransactionmanagement @enablescheduling @componentscan(basepackages = {"com.test"}) @configuration @enableneo4jrepositories(basepackages = "com.test.repositories") public class myneo4jconfiguration extends neo4jconfiguration { @override public sessionfactory getsessionfactory() { return new sessionfactory("com.test.movie"); } }
you have problem auto wiring: annotation on movieservice wrong, should be:
@service("movieservice")
or just
@service
you trying inject bean capitalised name, , context can't find (the injection done name). solution use @qualifier annotation on auto wired field,
@autowired @qualifier("movieservice") movieservice movieservice;