是否可以将 Neo4j OGM 转换器用作 Spring @Component?

Is it possible to use Neo4j OGM Converter as a Spring @Component?

我有一个注释为 @Component 的自定义转换器,它依赖于 Spring 在启动时通过 @Autowired 注入的 MyBean。

MyEntity 有一个字段 MyObject 要由该转换器转换。一旦我从 Neo4j 检索到 MyEntity,OGM 就会从 MyObjectToStringConverter 调用 toEntityAttribute。但是,我得到了 NPE。似乎 OGM 没有重用我由 Spring 创建的转换器,并且 MyBean 依赖项始终为 NULL。

OGM 是否应该能够从 Spring 上下文中获取我的转换器?

有没有办法让 OGM 初始化 MyBean?

如果没有,我可以自己注册一个转换器吗?

@NodeEntity
class MyEntity {

    @Convert(MyObjectToStringConverter.class)
    private MyObject myObject;
    ...
}

@Component
class MyObjectToStringConverter implements AttributeConverter<MyObject, String> {

    @Autowired
    private MyBean myBean;

    @Override
    public String toGraphProperty(MyObject value) {
        return value.getName();
    }

    @Override
    public MyObject toEntityAttribute(String value) {
        return myBean.convert(value); // myBean is NULL
    }
}

...
repository.find(myEntityID) // NPE from MyObjectToStringConverter


java.lang.NullPointerException: null
    at org.myproject.mvc.converter.MyObjectToStringConverter.toEntityAttribute(MyObjectToStringConverter.java:25) 
    at org.myproject.mvc.converter.MyObjectToStringConverter.toEntityAttribute(MyObjectToStringConverter.java:12) 
    at org.neo4j.ogm.entityaccess.FieldWriter.write(FieldWriter.java:64) 
    at org.neo4j.ogm.mapper.GraphEntityMapper.writeProperty(GraphEntityMapper.java:162) 
    at org.neo4j.ogm.mapper.GraphEntityMapper.setProperties(GraphEntityMapper.java:127) 
    at org.neo4j.ogm.mapper.GraphEntityMapper.mapNodes(GraphEntityMapper.java:108) 
    at org.neo4j.ogm.mapper.GraphEntityMapper.mapEntities(GraphEntityMapper.java:92) 
    at org.neo4j.ogm.mapper.GraphEntityMapper.map(GraphEntityMapper.java:67) 
    at org.neo4j.ogm.session.response.SessionResponseHandler.loadById(SessionResponseHandler.java:161) 
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:45) 
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:36) 
    at org.neo4j.ogm.session.Neo4jSession.load(Neo4jSession.java:99) 
    at org.springframework.data.neo4j.repository.GraphRepositoryImpl.findOne(GraphRepositoryImpl.java:60) 
    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:497) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy87.findOne(Unknown Source) 
    at org.myproject.mvc.service.AbstractService.find(AbstractService.java:40)

OGM 不会从 Spring 上下文中获取转换器 - 它完全不依赖于 Spring。也许您可以使用 Springs ConversionService?

http://docs.spring.io/spring-data/neo4j/docs/4.0.0.RELEASE/reference/html/#_spring_s_conversionservice