Micronaut FunctionInitializer覆盖应用程序属性

huangapple go评论79阅读模式
英文:

Micronaut FunctionInitializer override application properties

问题

@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?

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

    public TestFunction() {
    }

    public String execute() {
        return &quot;Hello&quot;;
    }

}

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(&quot;Initializing Lambda Container&quot;);
            this.dbCredentialService = new DBCredentialService();
            // Get updated database credential map
            Map&lt;String, Object&gt; 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?

答案1

得分: 1

如果您计划仅覆盖数据源凭据属性可以这样做

@Factory
public class HikariDataSourceFactory {

    @Bean
    @Primary
    public DataSource dataSource(DBCredentialService credentialService) throws URISyntaxException {

        Map&lt;String, Object&gt; credentials = this.dbCredentialService.getDbCredential();
        String username = &quot;user&quot;;
        String password = credentials.get(&quot;username&quot;);  

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(&quot;jdbc:postgresql://localhost:5432/postgres&quot;);
        config.setUsername(username);
        config.setPassword(password);
        config.setDriverClassName(&quot;org.postgresql.Driver&quot;);

        return new HikariUrlDataSource(config);
    }
}
英文:

If you plan to override only datasource credentials properties it could be done this way.

@Factory
public class HikariDataSourceFactory {

    @Bean
    @Primary
    public DataSource dataSource(DBCredentialService credentialService) throws URISyntaxException {

        Map&lt;String, Object&gt; credentials = this.dbCredentialService.getDbCredential();
        String username = &quot;user&quot;;
        String password = credentials.get(&quot;username&quot;);  
        
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(&quot;jdbc:postgresql://localhost:5432/postgres&quot;);
        config.setUsername(username);
        config.setPassword(password);
        config.setDriverClassName(&quot;org.postgresql.Driver&quot;);

        return new HikariUrlDataSource(config);
    }
}

huangapple
  • 本文由 发表于 2020年8月4日 19:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定