0

在SDN 4.1參考中,配置包括擴展Neo4jConfiguration並將Session對象顯式設置爲@Bean。在4.2中,指導原則是不擴展Neo4jConfiguration並遵循下面的配置。需要注意的是明確建立一個獨立的Session對象不存在:爲SDN 4.2自動裝配Neo4j OGM會話嵌入式

@Bean 
public org.neo4j.ogm.config.Configuration configuration() { 
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration(); 
    config 
      .driverConfiguration() 
      .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver") 
      .setURI(dbLocation); 
    return config; 

@Bean 
public SessionFactory sessionFactory() { 
    SessionFactory sessionFactory = new SessionFactory(configuration(), "org.my.package"); 
    sessionFactory.register(new MyPreSaveListener()); 
    return sessionFactory; 
} 

我已經看到了這個配置,同時在@Autowiring庫類Session對象本身(而不是工廠)使用。這是否意味着應用程序中只有一個Session實例?如果是這樣,這是否違背會話生命週期僅限於應用程序的「工作單元」的想法?

請注意,我的存儲庫是自定義的,不會擴展neo4j倉庫,因爲我目前正在使用Neo4jTemplate對象進行遷移。

回答

1

不,每個應用程序沒有一個會話。只要您的Session@Transactional註釋或TransactionTemplate內調用,它將調用代理Session(由Spring Data Neo4j框架在啓動期間生成)。這將有效地爲交易的生命週期(劃分的地方)創建一個會話,並且一旦超出範圍,允許它被垃圾收集。

+0

嘗試使用上面的配置自動裝入會話對象。收到「NoSuchBeanDefinitionException:沒有可用的bean類型'org.neo4j.ogm.session.Session'可用:期望至少有1個bean有資格作爲autowire候選者。」任何想法可以阻止從SessionFactory抓取Session實例? – ramenhood

+0

你有'@ EnabledNeo4jRepositories'和'@ EnabledTransactionManagement'嗎?您還必須確保自動裝配'Session'的地方是'@ Component's。 – digx1