What is the best way to inject values ​​from environment variables?

Consider this example

@Stateless
public class UniqueIdGenerator {
    private static final String COLON = ":";
    private String serverPrivateKey;

    @SuppressWarnings("UnusedDeclaration")
    public UniqueIdGenerator() {
    }

    @Inject
    public UniqueIdGenerator(@Nonnull final String serverPrivateKey) {
        this.serverPrivateKey = serverPrivateKey;
    }
    ...
}

I would like a @Injectvalue serverPrivateKeybased on an environment variable available in different environments.

What is the best way to enter it here?

+4
source share
3 answers

To enter values ​​from the environment rather than writing your own manufacturer methods, you may need to look at the Apache DeltaSpike configuration API.

Using the qualifier @ConfigProperty, you can enter values ​​from several property sources, such as system properties, environment variables, or JNDI.

Example:

@Inject
@ConfigProperty(name = "SERVER_PRIVATE_KEY")
private String serverPrivateKey;
+2
source

: bean, , . bean , (, System, ). , bean ( IPrivateKeyProvider).

UniqueIdGenerator, bean. , bean (- ).

+1

:

:

, . , :

  • [...]

.

0
source

All Articles