简体   繁体   English

Spring Neo4j-自动装配库返回null

[英]Spring Neo4j - autowire repository returns null

I'm fairly new to Spring (the Neo4j side), and I am having trouble @AutoWire -ing my repository. 我对Spring(Neo4j方面) @AutoWire ,并且在@AutoWire -ing我的存储库时遇到了麻烦。 this is my repo: 这是我的仓库:

package org.jarivm.relationGraph.objects.repositories;
public interface EmployeeRepository extends GraphRepository<Employee> {
    @Query("MATCH a=(:Employee)-[:WORKED_ON]->(p:Project) WHERE id(p)={0} RETURN a")
    Iterable<Employee> getTeamMates(Project client);
}

my test class: 我的测试课:

 package org.jarivm.relationGraph;

import org.apache.commons.collections4.set.ListOrderedSet;
import org.jarivm.relationGraph.objects.domains.Employee;
import org.jarivm.relationGraph.objects.domains.Project;
import org.jarivm.relationGraph.objects.repositories.EmployeeRepository;
import org.jarivm.relationGraph.utilities.NodeProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;


/**
 * @author Jari Van Melckebeke
 * @since 02.09.16
 */
@FixMethodOrder(MethodSorters.JVM)
public class Tests extends Application {
    @Autowired
    private Facade facade;
    @Autowired
    private EmployeeRepository employeeRepository;

    @Before
    public void setUp() throws Exception {
        facade = new Facade();
    }

    @After
    public void tearDown() throws Exception {
        facade.tearDown();
    }

/*
    @Test
    public void persistedEmployeeShouldBeRetrievableFromGraphDB() {
        Employee employee = new Employee("john", "adams");
        //System.out.println(session.getTransaction().status());
        if (!facade.findEmployeeByProperty("name", employee.getName()).iterator().hasNext()) {
            facade.commit(employee);

            Employee foundHim = facade.findEmployeeByProperty("name", employee.getName()).iterator().next();
            assert foundHim.getId().equals(employee.getId());
            assert foundHim.getName().equals(employee.getName());
        }
    }

    @Test
    public void persistedChainShouldBeRetrievableFromGraphDB() {
        Employee employee = new Employee("john", "myles");
        Client client = new Client();
        Sector sector = new Sector();
        Project project = new Project();
        client.setName("Real Dolmen");
        project.setClient(client);
        project.setCost(100.0);
        project.setName("project highrise");
        Set<Employee> set = new ListOrderedSet<Employee>();
        set.add(employee);
        project.setTeam(set);
        sector.setName("game");
        client.setSector(sector);
        facade.commit(sector);
        facade.commit(employee);
        facade.commit(client);
        facade.commit(project);

        Client foundHim = facade.findClientByProperty("name", client.getName()).iterator().next();
        assert foundHim.getId().equals(client.getId());
        assert foundHim.getName().equals(client.getName());
    }


    @Test
    public void projectShouldBeInsertableAlone() {
        Project project = new Project();
        project.setName("random");
        project.setLanguage("Java");
        facade.commit(project);

        Project foundHim = facade.findProjectByProperty("name", project.getName()).iterator().next();
        assert foundHim.getId().equals(project.getId());
    }

    @Test
    public void clientShouldBeInsertableAlone() {
        Client client = new Client();
        client.setName("Colruyt");

        facade.commit(client);

        Client foundHim = facade.findClientByProperty("name", client.getName()).iterator().next();
        assert foundHim.getId().equals(client.getId());
    }*/

    @Test
    public void createdNodesShoudBeEditable() {
        Iterator<Employee> employees = facade.findEmployeeByProperty("name", "john").iterator();
        Project project = facade.findProjectByProperty("name", "random").iterator().next();
        while (employees.hasNext()) {
            Employee e = employees.next();
            if (project.getTeam() == null)
                project.setTeam(new ListOrderedSet<Employee>());
            project.getTeam().add(e);
        }
        facade.commit(project);
    }


    package org.jarivm.relationGraph;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Test
    public void teamMatesShouldBeViewable() {
        Project p = facade.findProjectByProperty("name", "Matsoft").iterator().next();
        System.out.println(p);
        System.out.println(employeeRepository);
        Iterable<Employee> e = employeeRepository.getTeamMates(p);
        System.out.println(e.iterator());
    }
}

and my Application.java class: 和我的Application.java类:

package org.jarivm.relationGraph;

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
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.transaction.annotation.EnableTransactionManagement;

/**
 * @author Jari Van Melckebeke
 * @since 23.09.16
 */
@EnableTransactionManagement
@ComponentScan(basePackages = {"org.jarivm.relationGraph"})
@Configuration
@EnableNeo4jRepositories(basePackages = "org.jarivm.relationGraph.objects.repositories.EmployeeRepository")
public class Application extends Neo4jConfiguration {

public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";

@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
    config
            .driverConfiguration()
            .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
            .setURI(URL)
    .setCredentials("neo4j", "mypassword");
    return config;
}

@Override
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), "org.jarivm.relationGraph.objects.domains");
}
}

The @autowire did never work with this program so I do not know what the problem is... thank's in advance, Jari Van Melckebeke @autowire从未与该程序一起使用,所以我不知道问题出在哪里...在此先感谢,Jari Van Melckebeke

I think your Tests class should not extend Application, but instead be annotated with RunsWith - something like (untested): 我认为您的Tests类不应扩展Application,而应使用RunsWith进行注释-类似于(未测试):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=org.jarivm.relationGraph.Application.class, loader=AnnotationConfigContextLoader.class
public class Tests {

for more information, see the section titled Integration Testing with @Configuration Classes : 有关更多信息,请参见标题为使用@Configuration类进行集成测试的部分:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM