question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can we have @RefreshScope to refresh EnvironmentEndpoint bean at runtime

See original GitHub issue

In our application, we are creating EnvironmentEndpoint bean ourself as we need to override keysToSanitise.

We also want to refresh these keysToSanitise at run time when we have any update on property sources. So I have used @RefreshScope with it. But on application build I am getting below error:

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter@3c18b48e' method public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() to {[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}: There is already 'org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint@69e64aa8' bean method public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() mapped.

Below is my configuration:

 @Configuration
 @AutoConfigureBefore(EndpointAutoConfiguration.class)
 @EnableConfigurationProperties(EndpointProperties.class)
 public class EnvironmentEndpointAutoConfiguration {

private static final String SANITIZE_KEY_PREFIX = ".*";

@Bean
@RefreshScope
public EnvironmentEndpoint environmentEndpoint(ConfigurableEnvironment environment) {
    EnvironmentEndpoint environmentEndpoint = new EnvironmentEndpoint();
    final List<String> keysToSanitize = new ArrayList<>();
    MutablePropertySources propertySources = environment.getPropertySources();
    
   
        SystemEnvironmentPropertySource sampleSources = (SystemEnvironmentPropertySource) propertySources.get("sampleSources");
        for (String name : sampleSources.getPropertyNames()) {
            keysToSanitize.add(SANITIZE_KEY_PREFIX + name);
        }
    
    if (!keysToSanitize.isEmpty()) {
        keysToSanitize.addAll(defaultKeysToSanitize("password", "secret", "key", "token", ".*credentials.*", "vcap_services"));
        environmentEndpoint.setKeysToSanitize(keysToSanitize.toArray(new String[keysToSanitize.size()]));
    }
    return environmentEndpoint;
}

private List<String> defaultKeysToSanitize(String... keysToSanitize) {
    List<String> keysList = new ArrayList<>();
    for (int i = 0; i < keysToSanitize.length; i++) {
        keysList.add(keysToSanitize[i]);
    }
    return keysList;
}
 }

we are using spring boot 1.5.7 release version. I see some enhancement in spring boot 2.0 but that seems to be not released yet. We have external configuration using spring cloud consul to have refreshing of properties at runtime.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
spencergibbcommented, Oct 24, 2017

EnvironmentEndpoint is a boot thing, you’d need to go there to ask.

0reactions
JagmohanSharmacommented, Oct 26, 2017

As this is now being taken care in spring boot. I am closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reload the spring beans at runtime after hitting ...
Beans annotated with @RefreshScope aren't recreated right away. They are only recreated when one of their methods is called.
Read more >
Spring Cloud
The RefreshScope is a bean in the context and it has a public method refreshAll() to refresh all beans in the scope by...
Read more >
Apereo CAS - Keeping Configuration Fresh w/ @RefreshScope
The Spring application context at runtime can be refreshed using the actuator/refresh endpoint. We will need to enable and expose the ...
Read more >
Management & Monitoring - Micronaut Documentation
You can declare beans at runtime using interfaces. You can mark static methods as @Executable . A big HTTP client refactor. Spring integration...
Read more >
RefreshScope (Spring Cloud Commons Parent ... - javadoc.io
If you need to override the default id then provide an explicit id when the Scope is ... Flag to determine whether all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found