0
我有一個使用子/父上下文關係的Spring應用程序。其原因是爲了確保子上下文繼承父/子資源,然後添加更多的bean /資源以根據需要覆蓋它們。但是,當子上下文關閉時(退出try/catch範圍),它開始對其引用的所有bean進行清理,包括父範圍中的那些。這是不可取的,因爲我需要重用父上下文來創建另一個子上下文,但現在它是垃圾,因爲它包含一堆處置/關閉的bean。春天的孩子上下文關閉父
問題:
這是一個孩子的情況下所期望的行爲,它應該清理的父豆?如果是這樣,爲什麼?
我希望子上下文只清理它直接定義的bean,而不是繼承的bean。這可能嗎?
下面是一些相關的代碼:
private AbstractApplicationContext createChildContext(Path workspacePath, String catalogPath, boolean force, Map<String, String> buildOptions) {
// Set the properties to pass into the new context
Properties props=new Properties();
props.setProperty("workspacePath", workspacePath.toString());
props.setProperty("databasePath", workspacePath.toString() + File.separator + "data");
props.setProperty("catalog", catalogPath);
props.setProperty("force",String.valueOf(force));
PropertiesPropertySource pps=new PropertiesPropertySource("properties",props);
// Create new context
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.setParent(applicationContext);
context.getEnvironment().getPropertySources().addFirst(pps);
context.scan(Neo4jConfig.class.getPackage().getName());
context.register(Neo4jConfig.class);
ConfigurableListableBeanFactory beans = context.getBeanFactory();
BuildConfigurationService buildConfiguration = (BuildConfigurationService)beans.createBean(BuildConfigurationService.class);
buildConfiguration.setBuildConfiguration(buildOptions);
beans.registerSingleton("buildConfiguration", buildConfiguration);
context.refresh();
return context;
}
而在Neo4jConfig我們這樣做......
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableNeo4jRepositories
@EnableTransactionManagement
@EnableSpringConfigured
@EnableCaching(mode=AdviceMode.ASPECTJ)
@Import({ConversionServiceConfiguration.class})
public class Neo4jConfig extends Neo4jConfiguration {
...
@Bean GraphDatabaseService graphDatabaseService(@Value(value = "${databasePath}") String databasePath) {
logger.debug("Creating database using '{}' for the database path.",databasePath);
GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(databasePath);
...
這不應該發生,所以你必須做你的代碼奇怪/錯誤的東西。 –
喜歡什麼?有沒有開關來禁用它? –
不,如上所述,這不應該發生,所以你必須做一些奇怪的事情。沒有看到無法確定的代碼/配置/設置。 –