Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't inject complex configuration into any class #26

Open
marinasundstrom opened this issue Aug 29, 2023 · 0 comments
Open

I can't inject complex configuration into any class #26

marinasundstrom opened this issue Aug 29, 2023 · 0 comments

Comments

@marinasundstrom
Copy link

marinasundstrom commented Aug 29, 2023

I can't inject complex configuration into any class. Using Java 17.

I get this error when resolving MessagePublisher.java:

No configuration setting found for key 'rabbitmq'

My suspicion as that is has to do with the how the JSON is being read.

pom.xml:

    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>5.0.1</version>
    </dependency>
    <dependency>
      <groupId>net.jmob</groupId>
      <artifactId>guice.conf</artifactId>
      <version>v1.5.0</version>
    </dependency>

In BasicModule.java:

install(new ConfigurationModule()
                .fromPath(new File("/mypath")));

app.java:

@BindConfig(value = "app", syntax = Syntax.JSON)
public final class App {
    private static final int DEFAULT_RETENTIONTIMEDAYS = 2555;

    private MessagePublisher messagePubisher;

    @InjectConfig
    private Optional<Integer> retentionTimeDays;

    @Inject
    private App(MessagePublisher messagePubisher) {
        this.customerRetriever = customerRetriever;
        this.messagePubisher = messagePubisher;
    }
}

MessagePublisher.java:

@BindConfig(value = "app", syntax = Syntax.JSON)
public class MessagePublisher {
    @InjectConfig("rabbitmq")
    RabbitMQConnectionConfiguration configuration;
}

Assuming this app.json:

{
    "retentionTimeDays": 2000,
    "db": {
        "host": "localhost",
        "port": 3306,
        "database": "mydb",
        "user": "root",
        "password": "blah"
    },
    "rabbitmq": {
        "host": "localhost",
        "port": 1,
        "vhost": null,
        "exchange": "log"
    }
}
public interface RabbitMQConnectionConfiguration {

    String getHost();
    
    int getPort();

    String getVhost();

    String getExchange();
}
public interface DbConnectionConfiguration {

    String getHost();

    Optional<Integer> getPort();

    String getDatabase();

    String getUser();

    String getPassword();
}
@marinasundstrom marinasundstrom changed the title I can't inject configuration into any class I can't inject complex configuration into any class Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant