guice

How do I make an optional binding in Guice?

Here is my client: class Client { @Inject(optional=true) Service service; } Sometimes that Service isn't needed, and we know that information when the JVM starts (i.e before the binder is run). How do I make the binding optional? If I don't specify a binding at all it tries to new the Service (and fails because there is no zero-ar...

The decorator pattern and @Inject

When using Spring's based XML configuration, it's easy to decorate multiple implementations of the same interface and specify the order. For instance, a logging service wraps a transactional service which wraps the actual service. How can I achieve the same using the javax.inject annotations? ...

Guice servlet DSL, filter all but one URL

I want to filter all requests to my web application through my "SecurityFilter" which checks that a session variable "authToken" is valid. The problem is that in order to get this token you need to hit the "AuthServlet" which is at /auth. I need to filter all servlets except the /auth servlet with my "SecurityFilter". How can I do thi...

Strategies for reverse engineering project that uses Guice?

I have tried to understand dependency injection and not quite gotten it, except I have managed to pick up the understanding that it makes it hard to understand somebody else's code. :'( Anyway, I'm not sure how to briefly describe my problem but I will try. I am currently the sole coder working on a Java project that's been worked on b...

How to create annotation-configured beans with an existing instance of @Configuration?

Assume we have a simple @Configuration: @Configuration public class FooBarConfiguration { @Bean public Foo createFoo() { return new FooImpl(); } @Bean @Autowired public Bar createBar(Foo foo) { return new BarImpl(foo); } } This class can be used with AnnotationConfigApplicationContext to p...

Getting to grips with Google Guice

I'm just starting to play around with Google Guice as a dependency injection framework and am attempting to retrofit it to a small to medium size project I recently wrote. I understand the basics of how Guice works, but am a bit vague on some of the approach details. For example: 1) Modules are used to define your bindings, which are ...

What do I need to do to use Guice?

I want to learn Guice. I use eclipse. What to download? What to install? What to do in eclipse to use guice? Thanks. ...

When and where to use dependency injection with Guice?

Hi all, I've recently studied about Guice in a University course, and have seen the Google I/O video about it. In the video, they claim to use it in every Google project, including Wave, etc. I was wondering - is Guice really that ubiquitous? Is it really a must-know-must-use for programmers in Java? Should I always use it over a factor...

Guice dependency injection for entity beans?

For a rich domain driven design I want to use Guice dependency injection on JPA/Hibernate entity beans. I am looking for a similar solution as the Spring @configurable annotation for non-Spring beans. Does anybody know of a library? Any code examples? ...

Dead Code Detector for Guice/Gin modules?

Usual dead code detectors don't seem to find which defined objects in a Guice or Gin module are not needed any more: is there a simple way to detect this? For example, let's say I defined a Provider method for a type with a certain annotation: @Provides @Named("string range") String getStringRange(Request foo) { return foo.getBarPr...

Is there a way to inject final class with guice?

Hi, I have provider which should inject javax.mail.Session (provider looks it up in env. context of tomcat) as singleton. There is a problem when I use field injection somewhere in code: java.lang.IllegalArgumentException: Cannot subclass final class class javax.mail.Session Is there a way to go round this? Only clues that I've found ...

Using NetBeans profiler with Guice classes

How can I use the profiler from NetBeans 6.8 or 6.9 (choosing 'entire application') with guice enhanced classes? I am using google guice 2.0 (with warp persist 2.0-20090214) for some classes and wanted to profile those classes. But I cannot see a result for those classes. The only result I can see is for one method 'EnhancedClass.access...

Guice Servlet project fails with IllegalAccessException on startup

I'm using the Guice servlet module and trying to get just the basic filter and listener running. When I start my servlet container I get a java.lang.IllegalAccessException wrapped in an AssertionError. Basically what appears to be happening is that Guice is trying to instantiate com.google.inject.servlet.ManagedServletPipeline which is...

How does guice's TypeLiteral work?

How does Guice's TypeLiteral overcome the Java generic types erasure procedure? It works wonders but how is this accomplished? ...

Guice Inject Field in class not created by Guice

I have a class like so, that I create myself somewhere in my code: class StarryEyes { @Inject MyValidator validator; public StarryEyes(String name) { //.. } public doSomething() { // validator is NULL } } I want Guice to inject an instance of validator, which has a @Singleton annotation. I have a module th...

Google Guice 2.0

I have a class that loads some files into a specific object that itself contains several objects that contain different fields. Exmaple: class RootItem { public SubItemType1 sub1; } class SubItemType1 { public SubItemType2 sub2; public int data1; public float data2; } class SubItemType2 { public int data3; public boolean data4; } Al...

Global Google Guice module?

Is there a way to create a global guice module? so whenever someone calls the CreateInjector method of the Guice class it will get a specific "global" module? I am trying to do this because I have a set of compiled classes in a JAR that use the following: @Inject MyInterface.dosomething(); and I would like to be able to, in the proje...

Eclipse - Google Guice

I have been trying to use Google Guice in eclipse plug-in development. I have imported Guice jar as another plug-in project. But somehow Guice is unable to inject dependency. Any ideas??? This is the error message com.google.inject.ConfigurationException: Guice configuration errors: 1) No implementation for java.util.List<java.lang.S...

TestNG works fine for me but throws com.google.inject.internal.asm.util.TraceClassVisitor classnotfoundexception for other engineer

My ANT script runs without issue on my linux machine and on some xp VMs. When another engineer tries to kick it off locally, TestNG throws [testng] Caused by: java.lang.ClassNotFoundException: com.google.inject.internal.asm.util.TraceClassVisitor not found in (my libraries are all in here) I've never seen this class before and don't kno...

Guice vs AspectJ

I was working with GUice some months ago and now when I return to it, I find I have to reread the Guice documentation and examples to understand what I did with my code. However, when I look at AspectJ it is all too intuitive. It is an intuitive extension of the Java language. I feel I can sit down and write AspectJ code immediately alr...