Skip to content

Commit 9b824c8

Browse files
committed
simplify management of CouchbaseEnvironment bean
1 parent 1059329 commit 9b824c8

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AbstractCouchbaseConfiguration {
5656
*
5757
* @return the list of bootstrap hosts.
5858
*/
59-
protected abstract List<String> bootstrapHosts();
59+
protected abstract List<String> getBootstrapHosts();
6060

6161
/**
6262
* The name of the bucket to connect to.
@@ -73,39 +73,33 @@ public abstract class AbstractCouchbaseConfiguration {
7373
protected abstract String getBucketPassword();
7474

7575
/**
76-
* Override this method if you use Couchbase outside of the Spring context.
77-
* If non-null, defines the {@link CouchbaseEnvironment} to use for connection (it is your
78-
* responsibility to shutdown() it).
76+
* Is the {@link #getEnvironment()} to be destroyed by Spring?
7977
*
80-
* @return a pre-existing environment managed outside of Spring, or null if instead a managed
81-
* environment is to be used.
78+
* @return true if Spring should destroy the environment with the context, false otherwise.
8279
*/
83-
protected CouchbaseEnvironment sharedEnvironment() {
84-
return null; //assume most of the time we'll create a dedicated one
80+
protected boolean isEnvironmentManagedBySpring() {
81+
return true;
8582
}
8683

8784
/**
88-
* Override this method if you want a customized {@link CouchbaseEnvironment} but only
89-
* use it in the Spring context. This environment will be managed by Spring, which will
90-
* call its shutdown() method upon bean destruction.
85+
* Override this method if you want a customized {@link CouchbaseEnvironment}.
86+
* This environment will be managed by Spring, which will call its shutdown()
87+
* method upon bean destruction, unless you override {@link #isEnvironmentManagedBySpring()}
88+
* as well to return false.
9189
*
92-
* @return a customized environment to be managed by Spring, defaults to a {@link DefaultCouchbaseEnvironment}.
90+
* @return a customized environment, defaults to a {@link DefaultCouchbaseEnvironment}.
9391
*/
94-
protected CouchbaseEnvironment managedEnvironment() {
92+
protected CouchbaseEnvironment getEnvironment() {
9593
return DefaultCouchbaseEnvironment.create();
9694
}
9795

9896
@Bean(destroyMethod = "shutdown", name = BeanNames.COUCHBASE_ENV)
99-
@Conditional(ConfigurationCondition.class)
10097
public CouchbaseEnvironment couchbaseEnvironment() {
101-
CouchbaseEnvironment env = sharedEnvironment();
102-
if (env != null) {
103-
//the shared environment shouldn't have its shutdown method called when
104-
//the bean is destroyed, it is the responsibility of the user to destroy it.
105-
return new CouchbaseEnvironmentNoShutdownProxy(env);
106-
} else {
107-
return managedEnvironment();
98+
CouchbaseEnvironment env = getEnvironment();
99+
if (isEnvironmentManagedBySpring()) {
100+
return env;
108101
}
102+
return new CouchbaseEnvironmentNoShutdownProxy(env);
109103
}
110104

111105
/**
@@ -115,7 +109,7 @@ public CouchbaseEnvironment couchbaseEnvironment() {
115109
*/
116110
@Bean(destroyMethod = "disconnect", name = BeanNames.COUCHBASE_CLUSTER)
117111
public Cluster couchbaseCluster() throws Exception {
118-
return CouchbaseCluster.create(couchbaseEnvironment(), bootstrapHosts());
112+
return CouchbaseCluster.create(couchbaseEnvironment(), getBootstrapHosts());
119113
}
120114

121115
/**

src/main/java/org/springframework/data/couchbase/config/BeanNames.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class BeanNames {
4646
/**
4747
* Refers to the "<couchbase:translation-service />" bean
4848
*/
49-
static final String TRANSLATION_SERVICE = "translationService";
49+
static final String TRANSLATION_SERVICE = "couchbaseTranslationService";
5050

5151

5252
}

0 commit comments

Comments
 (0)