如何使用Spring Data Neo4j 4创建控制台应用程序?

时间:2015-10-09 00:41:42

标签: spring spring-mvc spring-boot spring-data-neo4j-4

我有一个控制台模块/应用程序(不是webapp),我想使用spring-data-neo4j构建的服务模块/应用程序。

控制台应用--->使用Spring Data Neo4j模块

我使用我认为是从Neo4jConfiguration继承配置会话,会话工厂和服务器(下面粘贴的代码)的标准方法。

当控制台应用程序尝试在spring-data-neo4j服务模块中使用ogm会话时,收到错误消息:

Caused by: java.lang.IllegalStateException: No Scope registered for scope 'session'
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:336)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
        at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:187)
        at com.sun.proxy.$Proxy51.loadAll(Unknown Source)
        at nz.co.thescene.core.member.MemberAccountService.loadMemberByEmailAddressPasswordAccount(MemberAccountService.java:95)
        at nz.co.thescene.console.menu.Menu.login(Menu.java:36)
        at nz.co.thescene.console.menu.Menu.login(Menu.java:98)
        at nz.co.thescene.console.menu.MainMenu.processUserInput(MainMenu.java:107)
        at nz.co.thescene.console.menu.Menu.processUserInput(Menu.java:82)
        at nz.co.thescene.console.ConsoleUI.run(ConsoleUI.java:64)
        at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
        ... 9 more

我的配置如下:

@Configuration
@ComponentScan("nz.co.*****")
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = "nz.co.*****")
@EnableConfigurationProperties(Neo4jProperties.class)
public class Neo4jConfig extends Neo4jConfiguration {

    private static final Logger log = LoggerFactory.getLogger(Neo4jConfig.class);

    @Inject
    private Neo4jProperties properties;

    @PostConstruct
    public void init() {
        log.debug("Initializing Neo4jConfig...");
    }

    @Bean
    @Override
    public Neo4jServer neo4jServer() {
        log.info("Initialising server connection");
        return new RemoteServer(properties.getUrl(), properties.getUsername(), properties.getPassword());
        //return new InProcessServer();
    }

    @Bean
    @Override
    public SessionFactory getSessionFactory() {
        log.info("Initialising Session Factory");
        return new SessionFactory("nz.co.*****");
    }

    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    @Override
    public Session getSession() throws Exception {
        log.info("Initialising session-scoped Session Bean");
        return super.getSession();
    }

}

我需要做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

由于控制台应用程序中没有会话bean的概念,因此删除

来自@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)

getSession()应该这样做。那么你甚至不需要覆盖getSession()