Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.data.neo4j;

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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.neo4j.bookmark.BeanFactoryBookmarkOperationAdvisor;
import org.springframework.data.neo4j.bookmark.BookmarkInterceptor;
import org.springframework.data.neo4j.bookmark.BookmarkManager;
import org.springframework.data.neo4j.bookmark.CaffeineBookmarkManager;
import org.springframework.web.context.WebApplicationContext;

/**
* Provides a {@link BookmarkManager} for Neo4j's bookmark support based on Caffeine if
* available. Depending on the applications kind (web or not) the bookmark manager will be
* bound to the application or the request, as recommend by Spring Data Neo4j.
*
* @author Michael Simons
* @since 2.1
*/
@Configuration
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
@ConditionalOnMissingBean(BookmarkManager.class)
@ConditionalOnBean({ BeanFactoryBookmarkOperationAdvisor.class,
BookmarkInterceptor.class })
class Neo4jBookmarkManagementConfiguration {

static final String BOOKMARK_MANAGER_BEAN_NAME = "bookmarkManager";

@Bean(BOOKMARK_MANAGER_BEAN_NAME)
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
@ConditionalOnWebApplication
public BookmarkManager requestScopedBookmarkManager() {
return new CaffeineBookmarkManager();
}

@Bean(BOOKMARK_MANAGER_BEAN_NAME)
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@ConditionalOnNotWebApplication
public BookmarkManager singletonScopedBookmarkManager() {
return new CaffeineBookmarkManager();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor;
import org.springframework.transaction.PlatformTransactionManager;
Expand All @@ -52,13 +53,15 @@
* @author Vince Bickers
* @author Stephane Nicoll
* @author Kazuki Shimizu
* @author Michael Simons
* @since 1.4.0
*/
@Configuration
@ConditionalOnClass({ SessionFactory.class, Neo4jTransactionManager.class,
PlatformTransactionManager.class })
@ConditionalOnMissingBean(SessionFactory.class)
@EnableConfigurationProperties(Neo4jProperties.class)
@Import(Neo4jBookmarkManagementConfiguration.class)
public class Neo4jDataAutoConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package org.springframework.boot.autoconfigure.data.neo4j;

import java.util.function.Predicate;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.assertj.core.api.Condition;
import org.junit.Test;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
Expand All @@ -29,13 +33,19 @@
import org.springframework.boot.autoconfigure.data.neo4j.country.Country;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.annotation.EnableBookmarkManagement;
import org.springframework.data.neo4j.bookmark.BookmarkManager;
import org.springframework.data.neo4j.mapping.Neo4jMappingContext;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -51,6 +61,7 @@
* @author Vince Bickers
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Michael Simons
*/
public class Neo4jDataAutoConfigurationTests {

Expand All @@ -69,6 +80,7 @@ public void defaultConfiguration() {
assertThat(context).hasSingleBean(SessionFactory.class);
assertThat(context).hasSingleBean(Neo4jTransactionManager.class);
assertThat(context).hasSingleBean(OpenSessionInViewInterceptor.class);
assertThat(context).doesNotHaveBean(BookmarkManager.class);
});
}

Expand Down Expand Up @@ -146,6 +158,41 @@ public void eventListenersAreAutoRegistered() {
});
}

@Test
public void providesARequestScopedBookmarkManangerIfNecessaryAndPossible() {
Predicate<ConfigurableApplicationContext> hasRequestScopedBookmarkManager = (
context) -> context.getBeanFactory() //
.getBeanDefinition("scopedTarget."
+ Neo4jBookmarkManagementConfiguration.BOOKMARK_MANAGER_BEAN_NAME) //
.getScope() //
.equals(WebApplicationContext.SCOPE_REQUEST);

this.contextRunner
.withUserConfiguration(BookmarkManagementEnabledConfiguration.class)
.run((context) -> assertThat(context)
.satisfies(new Condition<>(hasRequestScopedBookmarkManager,
"hasRequestScopedBookmarkManager")));
}

@Test
public void providesASingletonScopedBookmarkManangerIfNecessaryAndPossible() {
new ApplicationContextRunner()
.withUserConfiguration(TestConfiguration.class,
BookmarkManagementEnabledConfiguration.class)
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> assertThat(context)
.hasSingleBean(BookmarkManager.class));
}

@Test
public void doesNotProvideABookmarkManagerIfNotPossible() {
this.contextRunner.withClassLoader(new FilteredClassLoader(Caffeine.class))
.withUserConfiguration(BookmarkManagementEnabledConfiguration.class)
.run((context) -> assertThat(context)
.doesNotHaveBean(BookmarkManager.class));
}

private static void assertDomainTypesDiscovered(Neo4jMappingContext mappingContext,
Class<?>... types) {
for (Class<?> type : types) {
Expand Down Expand Up @@ -180,6 +227,12 @@ public org.neo4j.ogm.config.Configuration myConfiguration() {

}

@Configuration
@EnableBookmarkManagement
static class BookmarkManagementEnabledConfiguration {

}

@Configuration
static class EventListenerConfiguration {

Expand Down