簡體   English   中英

Spring Neo4j-自動裝配庫返回null

[英]Spring Neo4j - autowire repository returns null

我對Spring(Neo4j方面) @AutoWire ,並且在@AutoWire -ing我的存儲庫時遇到了麻煩。 這是我的倉庫:

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

我認為您的Tests類不應擴展Application,而應使用RunsWith進行注釋-類似於(未測試):

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

有關更多信息,請參見標題為使用@Configuration類進行集成測試的部分:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM