TitanDB的Spring Data Gremlin示例

时间:2016-08-03 16:54:36

标签: java spring spring-data titan gremlin

我正在尝试使用

https://github.com/gjrwebber/spring-data-gremlin

使用TitanDB。

TitanDBConfiguration.java:

@Slf4j
@Data
@Configuration
@EnableTransactionManagement
@EnableGremlinRepositories(basePackageClasses = {Application.class}, repositoryFactoryBeanClass = GremlinRepositoryFactoryBean.class)
class TitanDBConfiguration
{
@Bean GremlinRepository<Person> gremlinRepository(GremlinGraphFactory gremlinGraphFactory, GremlinGraphAdapter gremlinGraphAdapter, GremlinSchemaFactory gremlinSchemaFactory)
{
    GremlinSchema<Person> gremlinSchema = gremlinSchemaFactory.getSchema(Person.class);

    return new TitanGremlinRepository<>(gremlinGraphFactory, gremlinGraphAdapter, gremlinSchema);
}

@Bean GremlinBeanPostProcessor gremlinBeanPostProcessor(SchemaGenerator schemaGenerator)
{
    return new GremlinBeanPostProcessor(schemaGenerator, Application.class.getPackage().getName());
}

@Bean GremlinTransactionManager transactionManager(TitanGremlinGraphFactory titanGremlinGraphFactory)
{
    return new GremlinTransactionManager(titanGremlinGraphFactory);
}

@Bean TitanGremlinGraphFactory titanGremlinGraphFactory()
{
    TitanGremlinGraphFactory titanGremlinGraphFactory = new TitanGremlinGraphFactory();
    titanGremlinGraphFactory.setUrl("inmemory");
    titanGremlinGraphFactory.setUsername("admin");
    titanGremlinGraphFactory.setPassword("admin");

    return titanGremlinGraphFactory;
}

@Bean GremlinSchema<Person> gremlinSchema(GremlinSchemaFactory gremlinSchemaFactory)
{
    return gremlinSchemaFactory.getSchema(Person.class);
}

@Bean GremlinSchemaFactory gremlinSchemaFactory()
{
    return new GremlinSchemaFactory();
}

@Bean AnnotatedSchemaGenerator annotatedSchemaGenerator()
{
    return new JpaSchemaGenerator();
}

@Bean SchemaWriter schemaWriter()
{
    return new TitanSchemaWriter();
}

@Bean GremlinGraphAdapter gremlinGraphAdapter()
{
    return new TitanGraphAdapter();
}

@Bean GremlinRepositoryContext gremlinRepositoryContext
(
    GremlinGraphFactory gremlinGraphFactory,
    GremlinGraphAdapter gremlinGraphAdapter,
    GremlinSchemaFactory gremlinSchemaFactory,
    SchemaWriter schemaWriter
)
{
    return new GremlinRepositoryContext
    (
        gremlinGraphFactory,
        gremlinGraphAdapter,
        gremlinSchemaFactory,
        schemaWriter,
        TitanGremlinRepository.class
    );
}
}

PersonRepository.java:

@Repository
public interface PersonRepository extends GremlinRepository<Person>
{
}

Person.java:

@Vertex
public class Person
{
@Id
private String id;

@Property("name")
private String name;

@Link("lives_at")
private Address address;
}

我想做的测试:

public static void main(String[] args)
{
    PersonRepository personRepository = new AnnotationConfigApplicationContext(TitanDBConfiguration.class).getBean(PersonRepository.class);

    System.out.println(personRepository);
}

我得到的是:

0    [main] INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext  - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@198e2867: startup date [Wed Aug 03 18:30:42 CEST 2016]; root of context hierarchy
644  [main] WARN  org.springframework.context.annotation.ConfigurationClassEnhancer  - @Bean method TitanDBConfiguration.gremlinBeanPostProcessor is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
806  [main] INFO  org.reflections.Reflections  - Reflections took 103 ms to scan 2 urls, producing 7 keys and 10 values 
1579 [main] INFO  com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration  - Set default timestamp provider MICRO
1595 [main] INFO  com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration  - Generated unique-instance-id=7f00010131422-asd1
1620 [main] INFO  com.thinkaurelius.titan.diskstorage.Backend  - Initiated backend operations thread pool of size 8
1742 [main] INFO  com.thinkaurelius.titan.diskstorage.log.kcvs.KCVSLog  - Loaded unidentified ReadMarker start time 2016-08-03T16:30:44.726Z into com.thinkaurelius.titan.diskstorage.log.kcvs.KCVSLog$MessagePuller@5a18cd76
1873 [main] WARN  org.springframework.context.annotation.AnnotationConfigApplicationContext  - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gremlinRepositoryContext' defined in com.company.graphdatasource.configuration.TitanDBConfiguration: Unsatisfied dependency expressed through method 'gremlinRepositoryContext' parameter 2: Error creating bean with name 'gremlinSchemaFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gremlinSchemaFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gremlinRepositoryContext' defined in com.company.graphdatasource.configuration.TitanDBConfiguration: Unsatisfied dependency expressed through method 'gremlinRepositoryContext' parameter 2: Error creating bean with name 'gremlinSchemaFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gremlinSchemaFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at com.company.graphdatasource.Main.main(Main.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gremlinSchemaFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 19 more
Caused by: java.lang.NullPointerException
    at org.springframework.data.gremlin.schema.GremlinSchemaFactory.init(GremlinSchemaFactory.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 33 more

由于这是开源项目,我已经检查了什么是错误的:

@Component
public class GremlinSchemaFactory {

@Autowired
private Set<GremlinSchema> schemas;

private Map<String, GremlinSchema<?>> schemaMap = new HashMap<String, GremlinSchema<?>>();

@PostConstruct
public void init() {
    for (GremlinSchema schema : schemas) {
        String key = schema.getClassType().getSimpleName();
        if (schemaMap.containsKey(key)) {
            key = schema.getClassName();
        }
        schemaMap.put(key, schema);
    }
}

...

}

第27行(NullPointer在哪里)是:     String key = schema.getClassType()。getSimpleName();

正在抛出Nullpointer,因为模式是包含一个元素的LinkedHashSet - null。 这意味着,我的GremlinSchema没有注入Set模式,但我不知道为什么。 我测试过,我的

@Bean GremlinSchema<Person> gremlinSchema(GremlinSchemaFactory gremlinSchemaFactory)

正在成功创建。

所以我的@Bean正在创建,但没有注入,所以我考虑了包和Spring上下文,所以这里是上面的类的包:

com.company.Application有@Configuration和@ComponentScan

com.company.graphdatasource.configuration.TitanDBConfiguration(上面的注释)

com.company.graphdatasource.model.entity.Person(上面的注释)

com.company.graphdatasource.gremlin.PersonRepository(上面的注释)

上面看起来对我很好。

springDataGremlin = '0.1.1-SNAPSHOT'
enter code here
compile "com.github.gjrwebber:spring-data-gremlin-titan:${springDataGremlin}"
compile "com.github.gjrwebber:spring-data-gremlin-schemagen-jpa:${springDataGremlin}"

0 个答案:

没有答案