here are my code snippets.
here is my yml file:
productionServer: host: production-server.amazonaws.com publicIp: xx.xx.xx.xx privateIp: xx.xx.xx.xx userName: xx.xx.xx.xx password: xx.xx.xx.xx remoteFilePath: fake/path/ fileName: test.txt privateKey: private-public-key.ppk server: applicationConnectors: - type: http port: 8080 - type: https port: 8443 keyStorePath: key.keystore keyStorePassword: password validateCerts: false adminConnectors: - type: http port: 8081 - type: https port: 8444 keyStorePath: key.keystore keyStorePassword: password validateCerts: false
MyConfiguration Class:
import io.dropwizard.Configuration; public class MyConfiguration extends Configuration{ @NotNull @JsonProperty private ProductionServer productionServer;
Application Class:
import io.dropwizard.Application; public class MyApplication extends Application<MyConfiguration> { public static void main(String[] args) throws Exception{ new MysApplication().run(args); } @Override public String getName(){ return "micro-service"; } @Override public void initialize(Bootstrap<MyConfiguration> bootstrap){} @Override public void run(MyConfiguration conf, Environment environment ){ final MyResource myResource = new MyResource();
and when running this application:
i received registration as follows:
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[ io.dropwizard.jetty.HttpConnectorFactory@623e088f , io.dropwizard.jetty.HttpsConnectorFactory@39fcbef6 ], adminConnectors=[ io.dropwizard.jetty.HttpConnectorFactory@34f22f9d , io.dropwizard.jetty.HttpsConnectorFactory@77d67cf3 ], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[ io.dropwizard.logging.ConsoleAppenderFactory@663411de ]}} ==> com.mycompany.myproject.model.ProductionServer@5b04476e
means it successfully gets my yaml value. but my problem is introducing a DI or dependency on the MyConfiguration class. I cannot get the value of my ProductionServer, although the MyConfiguration object seems to be invalid in my service.
here is my code snippet linking MyService.class and MyConfiguration.class
DependencyBinder.class
import org.glassfish.hk2.utilities.binding.AbstractBinder;
public class DependencyBinder extends AbstractBinder {
@Override protected void configure() { bind(MyConfiguration.class).to(MyConfiguration.class); bind(MyService.class).to(MyService.class); }
MyService.class
public class MyService { @Inject MyConfiguration conf; public void invoke(){ System.out.println( "=============================== " ); System.out.println( "==> " + conf ); System.out.println("==> " + conf.getProductionServer() ); }
and during invoke () method call ... I got the log as follows:
=============================== ==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[ io.dropwizard.jetty.HttpConnectorFactory@34e82c4d ], adminConnectors=[ io.dropwizard.jetty.HttpConnectorFactory@19b70fbd ], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[ io.dropwizard.logging.ConsoleAppenderFactory@543f81c9 ]}} ==> null
now my problem is introducing the DI or dependencies of the MyConfiguration class in MyService.class. I cannot get the value of my ProductionServer, although the MyConfiguration object seems to be invalid in my service. please give me some permission? Thnx.