Mark Struberg created DELTASPIKE-1444:
-----------------------------------------

             Summary: Create POJO and Recors based Config
                 Key: DELTASPIKE-1444
                 URL: https://issues.apache.org/jira/browse/DELTASPIKE-1444
             Project: DeltaSpike
          Issue Type: New Feature
      Security Level: public (Regular issues)
          Components: Configuration
    Affects Versions: 1.9.5
            Reporter: Mark Struberg
            Assignee: Mark Struberg
             Fix For: 1.9.6


Right now we only support Interface based configuration with the 
{{@Configuration}} annotation. We could also support injecting POJOs directly 
and even Java14 Records.

E.g. a class 
{code}
public record Endpoint(String host, Integer port, String path){}
{code}
or with a parameter ct:
{code}
public class Endpoint {
     private final String host;
     ... 
    public Endpoint(
            @ConfigProperty(name="host") String host, 
            @ConfigProperty(name="port") Integer port, 
            @ConfigProperty(name="path") String path){
        this.host = host;
        ....
    }
}
{code}
Note that the {{@ConfigProperty}} annotation might get omitted if the class 
gets compiled with the {{javac -parameters}} option.

or with a default ct and field parameters:
{code}
public class Endpoint {
     private String host;
     private Integer port;
     private String path;
     ... 
}
{code}


Resolving of all the required properties must be performed atomically.
The solution might either be done via the built-in algorithms as shown above, 
in which case the following call might be used to denote that a bean should get 
resolved which consists of mulitple attributes:
{code}
ServerEndpointPojoWithCt someServer = 
ConfigResolver.resolve("myapp.some.server")
            .asBean(ServerEndpointPojoWithCt.class)
            .getValue();
{code} 
or via an explicit Converter function:
{code}
        final ServerEndpointPojoWithCt someServer = 
ConfigResolver.resolve("myapp.some.server")
            .asBean(ServerEndpointPojoWithCt.class, (cfg, key) -> new 
ServerEndpointPojoWithCt(
            cfg.resolve(key + ".host").getValue(),
            cfg.resolve(key + ".port").as(Integer.class).getValue(),
            cfg.resolve(key + ".path").getValue());)
            .getValue();

{code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to