'Spring Boot starter based on another starter with default configuration
I'm trying to do my own spring boot starter based on spring-boot-starter-web and spring-boot-starter-actuator. It is meant to provide easier integration with platform used in our company. In my starter I need some custom configuration for actuator which is normally done in application.yml file and it looks like this:
management:
endpoints:
web:
exposure.include:
- 'prometheus'
- 'health'
base-path: '/_meta'
path-mapping:
health: healthcheck
prometheus: metrics
metrics:
web:
server.request.autotime.percentiles: 0.50,0.75,0.90,0.99
client.request.autotime.percentiles: 0.50,0.75,0.90,0.99
But if I put configuration file in my starter it is overriden by config from actual application. So, the question is how can I provide custom configuration for actuator (or any other spring properties actually) in my starter?
Solution 1:[1]
It's been a while since I posted this question, but if someone came across same problem, here is solution I used:
create properties file with defaults in my starter (src/main/resources/starter-defaults.properties):
spring.main.banner-mode=off
spring.jmx.enabled=false
management.endpoints.jmx.exposure.exclude=*
management.endpoint.beans.enabled=false
management.endpoints.web.exposure.include=prometheus,health,info,g2aStandardHealth
management.endpoints.web.base-path=/_meta
management.endpoints.web.path-mapping.health=healthcheck
management.endpoints.web.path-mapping.prometheus=metrics
management.metrics.web.server.request.autotime.percentiles=0.50,0.75,0.90,0.99
management.metrics.web.client.request.autotime.percentiles=0.50,0.75,0.90,0.99
Add class (also to my starter):
public class DefaultConfigLoader implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigLoader.class);
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
try {
Properties props = new Properties();
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("starter-defaults.properties"));
event.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("starter-defaults", props));
} catch (IOException exception) {
logger.error("Unable to read defaults", exception);
}
}
}
And that's all. Default configuration will be loaded during application startup.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gaczm |
