racc / typesafeconfig-guice Goto Github PK
View Code? Open in Web Editor NEWAllows Guice Injection of configuration values from Typesafe Config
License: Apache License 2.0
Allows Guice Injection of configuration values from Typesafe Config
License: Apache License 2.0
For testing and for gradually retrofitting TypesafeConfig library into an existing codebase that has static or otherwise non-injectable config usages, it would be useful if there were an analog to Guice's built-in Names.named(String name) method to use in conjection with Guice's Key.get(Type type, Annotation annotation) method for querying the injector for specific named dependencies from the config.
Let's make a module:
public class TestModule extends AbstractModule {
@Override
protected void configure() {
Config config = ConfigFactory.load().getConfig("here.we.go");
install(TypesafeConfigModule.fromConfigWithPackage(config, "stuff.package"));
}
}
For some reason, we want to provide a String based on what's in the config (stupid example):
@Provides
@Singleton
private String someString( @TypesafeConfig("one") String one,
@TypesafeConfig("two") String two,
@TypesafeConfig("three") String three) {
return Optional.ofNullable(one)
.map( s -> two)
.orElse(three);
}
This will result in the following exception:
Jun 23, 2017 1:48:01 PM com.google.inject.internal.MessageProcessor visit
INFO: An exception was caught and reported. Message: org.reflections.ReflectionsException: Can't resolve member named 0 for class package.for.TestModule.lambda$someString
org.reflections.ReflectionsException: Can't resolve member named 0 for class package.for.TestModule..lambda$someString
Apparently the Reflections package counts the lambdas in the method as separate classes (which they are).
This, however works:
@Provides
@Singleton
private String someString( @TypesafeConfig("one") String one,
@TypesafeConfig("two") String two,
@TypesafeConfig("three") String three) {
if(one == null) {
return two;
} else {
return three;
}
}
I'm currently using finatra and it depends on guice:4.0
but this project depends on guice:4.0:no_aop
. Both dependencies seem to have the same file names and path but different contents(I'm new to guice ๐)
The problem comes when I try to package a fatjar using something like sbt-assembly
, it blows up because of the similar class names and paths.
/cc @racc
Your project, racc/typesafeconfig-guice
, depends on the outdated library FastClasspathScanner in the following source files:
src/main/java/com/github/racc/tscg/reflectors/fastclasspathscanner/FastClasspathScanningReflector.java
src/main/java/com/github/racc/tscg/TypesafeConfigModule.java
src/main/java/com/github/racc/tscg/reflectors/fastclasspathscanner/MethodIdentifier.java
FastClasspathScanner has been significantly reworked since the version your code depends upon:
ClassGraph is a significantly more robust library than FastClasspathScanner, and is more future-proof. All future development work will be focused on ClassGraph, and FastClasspathScanner will see no future development.
Please consider porting your code over to the new ClassGraph API, particularly if your project is in production or has downstream dependencies:
Feel free to close this bug report if this code is no longer in use. (You were sent this bug report because your project depends upon FastClasspathScanner, and has been starred by 52 users. Apologies if this bug report is not helpful.)
First of I want to say thanks for your great work here @racc!
We're considering using the typesafeconfig-guice library in our projects. One thing that we're interested in though is the ability to update the typesafe conf file and reload it on runtime in the app (without having to restart it). I'm curious to know what's your take on this.
Something like:
https://github.com/Netflix/archaius/tree/master/archaius-typesafe/src
Reproduced very simply via this library.
It looks like the only current workaround is to downgrade to 0.9.9.
I will try that and report back.
Hello Jason,
I'm using your library in my project and is working as expected but I have a little problem.
When I try to run my application as a stand-alone jar I'm getting this error.
Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:
1) No implementation for java.lang.Integer annotated with @com.github.racc.tscg.TypesafeConfig(value=server.instances) was bound.
while locating java.lang.Integer annotated with @com.github.racc.tscg.TypesafeConfig(value=server.instances)
for parameter 2 at com.myapp.infrastructure.conf.ServerConfig.<init>(ServerConfig.java:15)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:29)
2) No implementation for java.lang.Integer annotated with @com.github.racc.tscg.TypesafeConfig(value=server.port) was bound.
while locating java.lang.Integer annotated with @com.github.racc.tscg.TypesafeConfig(value=server.port)
for parameter 1 at com.myapp.infrastructure.conf.ServerConfig.<init>(ServerConfig.java:15)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:29)
3) No implementation for java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=amadeus.url) was bound.
while locating java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=amadeus.url)
for parameter 1 at com.myapp.infrastructure.conf.AmadeusConfig.<init>(AmadeusConfig.java:13)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:28)
4) No implementation for java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=amadeus.wsdl) was bound.
while locating java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=amadeus.wsdl)
for parameter 0 at com.myapp.infrastructure.conf.AmadeusConfig.<init>(AmadeusConfig.java:13)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:28)
5) No implementation for java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=server.host) was bound.
while locating java.lang.String annotated with @com.github.racc.tscg.TypesafeConfig(value=server.host)
for parameter 0 at com.myapp.infrastructure.conf.ServerConfig.<init>(ServerConfig.java:15)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:29)
6) No implementation for com.myapp.infrastructure.conf.hystrix.AbstractCommandConfig annotated with @com.github.racc.tscg.TypesafeConfig(value=hystrix.default.command) was bound.
while locating com.myapp.infrastructure.conf.hystrix.AbstractCommandConfig annotated with @com.github.racc.tscg.TypesafeConfig(value=hystrix.default.command)
for field at com.myapp.infrastructure.conf.hystrix.DefaultHystrixCommandConfig.commandConfig(DefaultHystrixCommandConfig.java:9)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:32)
7) No implementation for com.myapp.infrastructure.conf.hystrix.AbstractThreadPoolConfig annotated with @com.github.racc.tscg.TypesafeConfig(value=hystrix.default.threadPool) was bound.
while locating com.myapp.infrastructure.conf.hystrix.AbstractThreadPoolConfig annotated with @com.github.racc.tscg.TypesafeConfig(value=hystrix.default.threadPool)
for field at com.myapp.infrastructure.conf.hystrix.DefaultHystrixCommandConfig.threadPoolConfig(DefaultHystrixCommandConfig.java:9)
at com.myapp.infrastructure.CommonModule.configure(CommonModule.java:32)
The extrange thing is that I have not problems when I run my application with gradle using gradle run
command.
Here is one example of my configuration classes:
package com.copaair.connectmiles.infrastructure.conf;
import com.github.racc.tscg.TypesafeConfig;
import com.google.inject.Inject;
public class ServerConfig {
private final String host;
private final int port;
private final int instances;
@Inject
public ServerConfig(@TypesafeConfig("server.host") String host,
@TypesafeConfig("server.port") int port,
@TypesafeConfig("server.instances") int instances) {
this.host = host;
this.port = port;
this.instances = instances;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public int getInstances() {
return instances;
}
}
What am I missing?
Thank you very much!!!
It would be convenient to be able to register additional extractors into the config module.
In particular, this would be nice for using this library in Scala projects, where often the consuming code can more naturally be expressed using a Scala type instead of the Java types injected by this library (e.g. scala.concurrent.duration.Duration instead of java.Time.Duration, a scala collection instead of java.util.List).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.