In my micronaut microservice, I am trying to feed a property my.property.name
programatically instead of adding it to the application.yml. I have provided my code below and error I am getting. What should I do to fix this?
Code Approach-1:
import io.micronaut.context.annotation.Context;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.ActiveEnvironment;
import io.micronaut.context.env.PropertySource;
import io.micronaut.context.env.PropertySourceLoader;
import io.micronaut.core.io.ResourceLoader;
import io.micronaut.core.util.CollectionUtils;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
@Singleton
@Requires(beans = MyService.class)
@Context
public class MyPropertySourceLoader implements PropertySourceLoader {
private final MyService myService;
@Inject
public MyPropertySourceLoader(MyService myService) {
this.myService = myService;
}
@Override
public Optional<PropertySource> load(String resourceName, ResourceLoader resourceLoader) {
String myPropertyValue = myService.getProperty("id");
return Optional.of(PropertySource.of("custom-property", Collections.singletonMap("my.property.name", myPropertyValue), -10000));
}
@Override
public Optional<PropertySource> loadEnv(String resourceName, ResourceLoader resourceLoader, ActiveEnvironment activeEnvironment) {
String myPropertyValue = myService.getProperty("id");
return Optional.of(PropertySource.of("custom-property", Collections.singletonMap("my.property.name", myPropertyValue), -10000));
}
@Override
public Map<String, Object> read(String name, InputStream input) throws IOException {
String myPropertyValue =myService.getProperty("id");
return CollectionUtils.mapOf("my.property.name", myPropertyValue);
}
}
Code Approach-2:
import io.micronaut.context.annotation.Context;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.PropertySource;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.context.event.StartupEvent;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
@Singleton
@Context
@Order(Ordered.HIGHEST_PRECEDENCE) // Ensure this listener is invoked early
public class PropertyAddingListener implements ApplicationEventListener<StartupEvent> {
@Inject private Environment environment;
private final MyService myService;
@Inject
public PropertyAddingListener(MyService myService) {
this.myService = myService;
}
@Override
public void onApplicationEvent(StartupEvent event) {
Map<String, Object> properties = new HashMap<>();
properties.put("my.property.name", myService.getProperty("id"));
PropertySource propertySource = PropertySource.of("customSource", properties, -10000);
environment.addPropertySource(propertySource);
}
}
Error same in both approaches:
ERROR 2024-05-10 21:27:34,040 [main] io.micronaut.runtime.Micronaut: Error starting Micronaut server: Error instantiating bean of type [io.micronaut.configuration.kafka.config.DefaultKafkaConsumerConfiguration]
Message: Could not resolve placeholder ${my.propert.name}
Path Taken: new KafkaConsumerProcessor(……..
I have tried different approaches and but I am getting same error. I have tried increasing precedence of feeding the my.property.name
by using @Order(Ordered.HIGHEST_PRECEDENCE)
etc. but I am facing same error. What how to fix this issue, or is there any alternative approach to achieve this?