Commit 2add65fe authored by Phillip Webb's avatar Phillip Webb

Polish Neo4J BookmarkManager auto-configuration

Closes gh-14568
parent b7847d98
...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.data.neo4j; ...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.data.neo4j;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
...@@ -41,7 +40,7 @@ import org.springframework.web.context.WebApplicationContext; ...@@ -41,7 +40,7 @@ import org.springframework.web.context.WebApplicationContext;
* bound to the application or the request, as recommend by Spring Data Neo4j. * bound to the application or the request, as recommend by Spring Data Neo4j.
* *
* @author Michael Simons * @author Michael Simons
* @since 2.1 * @since 2.1.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class }) @ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
...@@ -50,17 +49,16 @@ import org.springframework.web.context.WebApplicationContext; ...@@ -50,17 +49,16 @@ import org.springframework.web.context.WebApplicationContext;
BookmarkInterceptor.class }) BookmarkInterceptor.class })
class Neo4jBookmarkManagementConfiguration { class Neo4jBookmarkManagementConfiguration {
static final String BOOKMARK_MANAGER_BEAN_NAME = "bookmarkManager"; private static final String BOOKMARK_MANAGER_BEAN_NAME = "bookmarkManager";
@Bean(BOOKMARK_MANAGER_BEAN_NAME) @Bean(BOOKMARK_MANAGER_BEAN_NAME)
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
@ConditionalOnWebApplication @ConditionalOnWebApplication
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
public BookmarkManager requestScopedBookmarkManager() { public BookmarkManager requestScopedBookmarkManager() {
return new CaffeineBookmarkManager(); return new CaffeineBookmarkManager();
} }
@Bean(BOOKMARK_MANAGER_BEAN_NAME) @Bean(BOOKMARK_MANAGER_BEAN_NAME)
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@ConditionalOnNotWebApplication @ConditionalOnNotWebApplication
public BookmarkManager singletonScopedBookmarkManager() { public BookmarkManager singletonScopedBookmarkManager() {
return new CaffeineBookmarkManager(); return new CaffeineBookmarkManager();
......
...@@ -53,7 +53,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; ...@@ -53,7 +53,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* @author Vince Bickers * @author Vince Bickers
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Kazuki Shimizu * @author Kazuki Shimizu
* @author Michael Simons
* @since 1.4.0 * @since 1.4.0
*/ */
@Configuration @Configuration
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package org.springframework.boot.autoconfigure.data.neo4j; package org.springframework.boot.autoconfigure.data.neo4j;
import java.util.function.Predicate;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import org.assertj.core.api.Condition;
import org.junit.Test; import org.junit.Test;
import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory; import org.neo4j.ogm.session.SessionFactory;
...@@ -27,6 +24,7 @@ import org.neo4j.ogm.session.event.Event; ...@@ -27,6 +24,7 @@ import org.neo4j.ogm.session.event.Event;
import org.neo4j.ogm.session.event.EventListener; import org.neo4j.ogm.session.event.EventListener;
import org.neo4j.ogm.session.event.PersistenceEvent; import org.neo4j.ogm.session.event.PersistenceEvent;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.neo4j.city.City; import org.springframework.boot.autoconfigure.data.neo4j.city.City;
...@@ -36,7 +34,6 @@ import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfigu ...@@ -36,7 +34,6 @@ import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfigu
import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -160,18 +157,14 @@ public class Neo4jDataAutoConfigurationTests { ...@@ -160,18 +157,14 @@ public class Neo4jDataAutoConfigurationTests {
@Test @Test
public void providesARequestScopedBookmarkManangerIfNecessaryAndPossible() { public void providesARequestScopedBookmarkManangerIfNecessaryAndPossible() {
Predicate<ConfigurableApplicationContext> hasRequestScopedBookmarkManager = (
context) -> context.getBeanFactory() //
.getBeanDefinition("scopedTarget."
+ Neo4jBookmarkManagementConfiguration.BOOKMARK_MANAGER_BEAN_NAME) //
.getScope() //
.equals(WebApplicationContext.SCOPE_REQUEST);
this.contextRunner this.contextRunner
.withUserConfiguration(BookmarkManagementEnabledConfiguration.class) .withUserConfiguration(BookmarkManagementEnabledConfiguration.class)
.run((context) -> assertThat(context) .run((context) -> {
.satisfies(new Condition<>(hasRequestScopedBookmarkManager, BeanDefinition bookmarkManagerBean = context.getBeanFactory()
"hasRequestScopedBookmarkManager"))); .getBeanDefinition("scopedTarget.bookmarkManager");
assertThat(bookmarkManagerBean.getScope())
.isEqualTo(WebApplicationContext.SCOPE_REQUEST);
});
} }
@Test @Test
...@@ -181,8 +174,11 @@ public class Neo4jDataAutoConfigurationTests { ...@@ -181,8 +174,11 @@ public class Neo4jDataAutoConfigurationTests {
BookmarkManagementEnabledConfiguration.class) BookmarkManagementEnabledConfiguration.class)
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class)) TransactionAutoConfiguration.class))
.run((context) -> assertThat(context) .run((context) -> {
.hasSingleBean(BookmarkManager.class)); assertThat(context).hasSingleBean(BookmarkManager.class);
assertThat(context.getBeanDefinitionNames())
.doesNotContain("scopedTarget.bookmarkManager");
});
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment