Home:ALL Converter>Micronaut FunctionInitializer override application properties

Micronaut FunctionInitializer override application properties

Ask Time:2020-08-04T19:21:32         Author:Sujith C P

Json Formatter

@Singleton
public class TestFunction extends FunctionInitializer {
    Logger log = LoggerFactory.getLogger(TestFunction.class);

    public TestFunction() {
    }

    public String execute() {
        return "Hello";
    }

}

I want to override datasource properties in application.yml file programmatically, but without using bean created event listener. Is there a way to do that. Like creating a custom application context with properties.

I have used the below approach for Micronaut API gateway proxy.

public class StreamLambdaHandler implements RequestStreamHandler {
.......
public StreamLambdaHandler() {
        try {
            log.info("Initializing Lambda Container");
            this.dbCredentialService = new DBCredentialService();
            // Get updated database credential map
            Map<String, Object> props = this.dbCredentialService.getDbCredential();
            // Create application context builder with updated properties
            // i.e Override datasources properties in application.yml
            builder = ApplicationContext.build().properties(props);
            handler = new MicronautLambdaContainerHandler(builder);
      }....
    ........
}

Can we do something similar with FunctionInitializer?

Author:Sujith C P,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63245843/micronaut-functioninitializer-override-application-properties
yy