Results 1 to 6 of 6

Thread: Neo4j: Neo4jTemplate is not autowiring

  1. #1
    Join Date
    Aug 2004
    Posts
    218

    Default Neo4j: Neo4jTemplate is not autowiring

    I've create a small project to import data from a CSV into Neo4j. I started by creating a test but when it runs my ImportService class, it is unable to autowire the Neo4jTemplate. I found this thread (http://stackoverflow.com/questions/1...spring-project) which is similar to mine but I'm not using these old versions. Any idea why the template is not getting autowired so I can save to Neo4j?

    Here's what I have

    pom.xml:
    Code:
        <properties>
            <target.jdk>1.7</target.jdk>
            <spring.data.version>2.2.0.RELEASE</spring.data.version>
            <spring.version>3.1.4.RELEASE</spring.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-neo4j</artifactId>
                <version>${spring.data.version}</version>
            </dependency>
            <dependency>
                <groupId>net.sourceforge.javacsv</groupId>
                <artifactId>javacsv</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>3.2.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
        </dependencies>
    Here is my applicationContext.xml (under src/main/resources):

    Code:
        <context:annotation-config/>
        <context:spring-configured/>
        <context:component-scan base-package="com.qualcomm.ea.ssat.poc.neo4j"/>
    
        <tx:annotation-driven mode="proxy"/>
    
        <bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
                destroy-method="shutdown">
            <constructor-arg index="0" value="target/config-test"/>
        </bean>
    
        <neo4j:config graphDatabaseService="graphDatabaseService"/>
    Here is my test:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:**/applicationContext.xml"})
    public class ImportServiceTest {
        private ImportService importer = new ImportService();
    
        @Test
        public void test() {
            CsvReader stationsFile = null;
            try {
                stationsFile = new CsvReader("C:\\dev\\spring-neo4j\\src\\main\\resources\\community_follows.csv");
                stationsFile.readHeaders();
                importer.importCommunities(stationsFile);
            } catch (Exception e) {
                e.printStackTrace(); 
            } finally {
                stationsFile.close();
            }
    
        }
    }
    Here's my ImportService:

    Code:
    @Service
    public class ImportService {
        @Autowired
        private Neo4jTemplate template;
    
        private Logger log = LoggerFactory.getLogger(ImportService.class);
    
        @Transactional
        public void importCommunities(CsvReader communityFile) throws IOException, ParseException {
            DateFormat dt = new SimpleDateFormat("dd-MMM-yy");
            while (communityFile.readRecord()) {
                //CREATED,LAST_MODIFIED,GROUPID,COMMUNITY,DISPLAYNAME,FOLLOWER,FIRSTNAME,LASTNAME,DESCRIPTION
                String comm = communityFile.get(3);
                int id = Integer.parseInt(communityFile.get(2));
                Date created = dt.parse(communityFile.get(0));
                Date modified = dt.parse(communityFile.get(1));
                String dname = communityFile.get(4);
                String description = communityFile.get(8);
    
                Community community = new Community(id, comm, dname, created, modified, description);
                template.save(community);  // <<<<<<<<< Template is NULL
    
            }
        }
    }

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    And why should it? You aren't using the ImportService from the applicationcontext but are creating your own instance... So basically the test runs without spring (you would get the same effect when you remote the @RunWith and @ContextConfiguration annotations).

    Use @Autowired on the ImportService to have it injected by spring instead of constructing your own instance.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2004
    Posts
    218

    Default

    Yes, agreed. I had it Autowired before in my test but that resulted in this:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.qualcomm.ea.ssat.poc.neo4j.tests.ImportServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qualcomm.ea.ssat.poc.neo4j.service.ImportService com.qualcomm.ea.ssat.poc.neo4j.tests.ImportServiceTest.importer; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.qualcomm.ea.ssat.poc.neo4j.service.ImportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qualcomm.ea.ssat.poc.neo4j.service.ImportService com.qualcomm.ea.ssat.poc.neo4j.tests.ImportServiceTest.importer; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.qualcomm.ea.ssat.poc.neo4j.service.ImportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    	... 24 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.qualcomm.ea.ssat.poc.neo4j.service.ImportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:947)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:816)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
    That doesn't make sense to me because I have the component-scan that should pickup the @Service on my bean.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    For starters I suggest you quit mixing spring versions (3.1.4 and 3.2.1 at least judging from your configuration).

    If it isn't autowired it isn't detected which would indicate an error in your configuration. Enable trace logging and figure out what is happening/which beans get loaded.

    Your configuration has context:annotation-config which is already implied by the component-scan. Not sure why you have a spring-configured in there unless you are using @Configurable and have the agents etc. setup for that.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2004
    Posts
    218

    Default

    Quote Originally Posted by Marten Deinum View Post
    Your configuration has context:annotation-config which is already implied by the component-scan. Not sure why you have a spring-configured in there unless you are using @Configurable and have the agents etc. setup for that.
    I pulled that from Spring Data examples on Github. That doesn't make a difference though. Moreover, when I brought in hello-worlds example and tried to run it in STS or Maven, it suffers from the same issue. <sigh>
    Last edited by Loumeister; Feb 28th, 2013 at 07:57 PM.

  6. #6
    Join Date
    Aug 2004
    Posts
    218

    Default

    It ends up being that you must not use ANY wildcards in your @ContextConfiguration. So this simple change overcame this issue:


    @ContextConfiguration(locations = "classpath:applicationContext.xml")

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •