Guice injection failing with guice annotation module
up vote
0
down vote
favorite
I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.
The code itself is a jax-rs web service where I pass 2 header parameters userId
and adminId
and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId
is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.
The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.
Very Basic Code
Lowest Class called for database calls
public class lowestClass
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);
@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database
Lowest Class Module
public class lowestClassModule
public final String adminId;
public final String userId;
public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
UpperClass
public class upperClass
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object
//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;
public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));
Guice Annotation
Annotation Interface
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement
Annotation Interceptor
public class LogTimerMeasurementInterceptor implements MethodInterceptor
private final String userId;
public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
final I18N supplyI18n = I18NFactory.createI18N(this.getClass());
final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);
Annotation Module
public class TimerBindingModule extends AbstractModule
private final String userId;
public TimerBindingModule(final String userId)
this.userId = userId;
@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));
java timer annotations guice
add a comment |
up vote
0
down vote
favorite
I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.
The code itself is a jax-rs web service where I pass 2 header parameters userId
and adminId
and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId
is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.
The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.
Very Basic Code
Lowest Class called for database calls
public class lowestClass
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);
@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database
Lowest Class Module
public class lowestClassModule
public final String adminId;
public final String userId;
public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
UpperClass
public class upperClass
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object
//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;
public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));
Guice Annotation
Annotation Interface
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement
Annotation Interceptor
public class LogTimerMeasurementInterceptor implements MethodInterceptor
private final String userId;
public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
final I18N supplyI18n = I18NFactory.createI18N(this.getClass());
final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);
Annotation Module
public class TimerBindingModule extends AbstractModule
private final String userId;
public TimerBindingModule(final String userId)
this.userId = userId;
@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));
java timer annotations guice
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
Also, you probably shouldn’t callinvocation.proceed()
in thecatch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
– Matthew Pope
Nov 24 at 5:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.
The code itself is a jax-rs web service where I pass 2 header parameters userId
and adminId
and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId
is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.
The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.
Very Basic Code
Lowest Class called for database calls
public class lowestClass
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);
@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database
Lowest Class Module
public class lowestClassModule
public final String adminId;
public final String userId;
public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
UpperClass
public class upperClass
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object
//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;
public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));
Guice Annotation
Annotation Interface
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement
Annotation Interceptor
public class LogTimerMeasurementInterceptor implements MethodInterceptor
private final String userId;
public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
final I18N supplyI18n = I18NFactory.createI18N(this.getClass());
final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);
Annotation Module
public class TimerBindingModule extends AbstractModule
private final String userId;
public TimerBindingModule(final String userId)
this.userId = userId;
@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));
java timer annotations guice
I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.
The code itself is a jax-rs web service where I pass 2 header parameters userId
and adminId
and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId
is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.
The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.
Very Basic Code
Lowest Class called for database calls
public class lowestClass
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);
@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database
Lowest Class Module
public class lowestClassModule
public final String adminId;
public final String userId;
public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
UpperClass
public class upperClass
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object
//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;
public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;
public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;
@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));
Guice Annotation
Annotation Interface
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement
Annotation Interceptor
public class LogTimerMeasurementInterceptor implements MethodInterceptor
private final String userId;
public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
final I18N supplyI18n = I18NFactory.createI18N(this.getClass());
final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);
Annotation Module
public class TimerBindingModule extends AbstractModule
private final String userId;
public TimerBindingModule(final String userId)
this.userId = userId;
@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));
java timer annotations guice
java timer annotations guice
edited Nov 11 at 16:42
asked Nov 10 at 17:34
TOTOROCATBUS
387
387
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
Also, you probably shouldn’t callinvocation.proceed()
in thecatch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
– Matthew Pope
Nov 24 at 5:19
add a comment |
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
Also, you probably shouldn’t callinvocation.proceed()
in thecatch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
– Matthew Pope
Nov 24 at 5:19
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
Also, you probably shouldn’t call
invocation.proceed()
in the catch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.– Matthew Pope
Nov 24 at 5:19
Also, you probably shouldn’t call
invocation.proceed()
in the catch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.– Matthew Pope
Nov 24 at 5:19
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241629%2fguice-injection-failing-with-guice-annotation-module%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07
Also, you probably shouldn’t call
invocation.proceed()
in thecatch
block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.– Matthew Pope
Nov 24 at 5:19