Spring Neo4j - autowire存储库返回null

时间:2016-09-27 15:51:11

标签: java spring interface neo4j annotations

我是Spring(Neo4j方面)的新手,我在使用我的存储库时遇到了麻烦@AutoWire。 这是我的回购:

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);
}

我的考试班:

 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());
    }
}

和我的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");
}
}

@autowire从未使用过这个程序,所以我不知道问题是什么...... 提前致谢, Jari Van Melckebeke

1 个答案:

答案 0 :(得分:0)

我认为您的Tests类不应该扩展Application,而是使用RunsWith注释 - 类似于(未经测试):

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

有关详细信息,请参阅标题为使用@Configuration类进行集成测试的部分: