Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit f85169b

Browse files
authored
Document how to make beans refreshable (#2613)
1 parent 7ab8b2c commit f85169b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/src/main/asciidoc/core.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,39 @@ public interface GcpEnvironmentProvider {
142142
}
143143
----
144144

145+
=== Customizing bean scope
146+
Spring Cloud GCP starters autoconfigure all necessary beans in the default singleton scope.
147+
If you need a particular bean or set of beans to be recreated dynamically (for example, to rotate credentials), there are two options:
148+
149+
. Annotate custom beans of the necessary types with `@RefreshScope`.
150+
This makes the most sense if your application is already redefining those beans.
151+
. Override the scope for autoconfigured beans by listing them in the Spring Cloud property `spring.cloud.refresh.extra-refreshable`.
152+
+
153+
For example, the beans involved in Cloud Pub/Sub subscription could be marked as refreshable as follows:
154+
[source,properties]
155+
----
156+
spring.cloud.refresh.extra-refreshable=org.springframework.cloud.gcp.pubsub.support.SubscriberFactory,\
157+
org.springframework.cloud.gcp.pubsub.core.subscriber.PubSubSubscriberTemplate
158+
----
159+
160+
[NOTE]
161+
====
162+
`SmartLifecycle` beans, such as Spring Integration adapters, do not currently support `@RefreshScope`.
163+
If your application refreshes any beans used by such `SmartLifecycle` objects, it may also have to restart the beans manually when `RefreshScopeRefreshedEvent` is detected, such as in the Cloud Pub/Sub example below:
164+
165+
[source,java]
166+
----
167+
@Autowired
168+
private PubSubInboundChannelAdapter pubSubAdapter;
169+
170+
@EventListener(RefreshScopeRefreshedEvent.class)
171+
public void onRefreshScope(RefreshScopeRefreshedEvent event) {
172+
this.pubSubAdapter.stop();
173+
this.pubSubAdapter.start();
174+
}
175+
----
176+
====
177+
145178
=== Spring Initializr
146179

147180
This starter is available from https://start.spring.io/[Spring Initializr] through the `GCP Support` entry.

0 commit comments

Comments
 (0)