在启动具有Cassandra配置和Neo4j配置的弹簧容器时,豆子正在混淆容器

时间:2018-04-09 14:01:17

标签: spring neo4j cassandra

I started up a spring container with both cassandra configuration and spring data neo4j configuration, but while starting up, following exception is noticed:

    Could not convert argument value of type [org.springframework.data.cassandra.core.cql.session.DefaultSessionFactory] to required type [org.neo4j.ogm.session.SessionFactory] exception

Any idea about why does this error occur?
  

我只是想使用相同的弹簧容器将一组实体保存到Cassandra和Neo4j。所需的配置是   如以下所说的。豆名称是合格的,在某些情况下,   某些方法在Cassandra Configuration中被覆盖为no   无济于事。还发现Neo4j是硬编码的,可以将豆子当作   了getSessionFactory。

*Following are my configurations:*

Neo4j配置:

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = { "com.*.repo"})
public class Neo4jConfig {

    @Resource
    Environment environment;


    @Bean(name="getSessionFactory")
    @Lazy
    public SessionFactory getSessionFactory(@Qualifier(value="neo4jConfigurationBean") 
    org.neo4j.ogm.config.Configuration configuration) throws ConfigurationReadManagerFailureException {
        String[] packages = { "com.entity.store.reflection.state",
                "com.entity.store.reflection.base" };
        SessionFactory neo4jSessionFactory = new SessionFactory(configuration, packages);
        neo4jSessionFactory.setLoadStrategy(LoadStrategy.SCHEMA_LOAD_STRATEGY);
        return neo4jSessionFactory;
    }

    @Bean(name="neo4jTransactionManager")
    @Lazy
    public Neo4jTransactionManager getTransactionManager(@Qualifier(value="getSessionFactory")SessionFactory sessionFactory) 
            throws ConfigurationReadManagerFailureException {
        return new Neo4jTransactionManager(sessionFactory);
    }

    @Primary
    @Bean(name="getSession")
    @Lazy
    public Session getSession(@Qualifier(value="neo4jTransactionManager") Neo4jTransactionManager txManager) 
            throws ConfigurationReadManagerFailureException {
        return txManager.getSessionFactory().openSession();
    }

    @Bean(name="neo4jConfigurationBean")
    @Lazy
    public org.neo4j.ogm.config.Configuration getConfiguration() throws ConfigurationReadManagerFailureException {
        ConfigurationSource config = new Neo4jConfigurationSource();
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder(config)
                .build();
        return configuration;
    }

}

Cassandra配置:

@Configuration
@EnableCassandraRepositories(basePackages = {"com.*.repo"})
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Resource
    public Environment environment;

    @Override
    protected String getKeyspaceName() {
        return environment.getProperty("cassandra.keyspace");
    }

    @Override
    @Bean
    @Lazy
    public CassandraClusterFactoryBean cluster() {


        final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
        cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
        cluster.setSocketOptions( new SocketOptions()
        .setConnectTimeoutMillis(60000)
        .setReadTimeoutMillis(60000));
        cluster.setRetryPolicy(new CustomRetryPolicy(10, 10, 10));
        return cluster;
    }


    @Bean
    @Lazy
    public CassandraMappingContext mappingContext() throws ClassNotFoundException {
        CassandraMappingContext mappingContext = new CassandraMappingContext();
        List<String> entityBasePackages=new ArrayList<String>();
        entityBasePackages.add(environment.getProperty("cassandra.package.entity"));
        mappingContext.setInitialEntitySet(CassandraEntityClassScanner.scan(entityBasePackages));
        mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(),getKeyspaceName()));
        return mappingContext;
    }

    @Bean
    @Lazy
    public CassandraConverter converter() throws ClassNotFoundException  {


        return new MappingCassandraConverter(mappingContext());
    }

    @Bean
    @Lazy
    public CassandraSessionFactoryBean session()  { 
        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(getKeyspaceName());
        try {
            session.setConverter(converter());
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
        session.setSchemaAction(SchemaAction.CREATE_IF_NOT_EXISTS);
        return session;
    }



    @Bean(name="cassandraTemplate")
    @Lazy
    public CassandraTemplate getCassandraTemplate() throws Exception {

        CassandraTemplate cop= new CassandraTemplate(session().getObject());
        CassandraMappingContext mappingContext = new CassandraMappingContext();
        mappingContext=(CassandraMappingContext) cop.getConverter().getMappingContext();
        mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(),getKeyspaceName()));
        return cop;
    }
}

1 个答案:

答案 0 :(得分:0)

我们通过实现自己的cassandra Configuration而不是扩展AbstractCassandraConfiguration来解决此问题,Cassandra和Neo4j Session使用的bean名称是相同的。 Neo4j被硬编码为将getSession作为bean,而AbstractCassandraConfiguration使用相同的bean签名。当我们实现自己的配置时,问题得以解决,应用程序正常运行。