If you absolutely want to go with just in time bindings: @ProvidedBy
is your best bet.

Cheers
Alen

On 5 jul., 09:33, Rainer Jung <[email protected]> wrote:
> Hello everyone.
>
> I'm trying to create a Singleton to be used by Guice, and still remain to
> be used as classical Singleton (it's a new jar for a bigger project, and we
> currently don't support DI, but we will in the future).
> So i have a Interface like
> @ImplementedBy(MyClassImpl.class)
> public interface IMyClass {
>     boolean returnTrue();}
>
> I now have the implementation with a private constructor:
> @Singleton
> public class MyClassImpl implements IMyClass {
>     private MyClassImpl() {}
>     @Provides
>     @Singleton
>     public static MyClassImpl getInstance() {
>         if (instance == null) {
>             instance = new PermissionDecisionMakerImpl();
>         }
>         return instance;
>     }
>     @Override
>     boolean returnTrue() {
>         return true;
>     }}
>
> As you see, i already tried with the Annotations to have the Singleton to
> be automaticly found by the Injector.
> When i do:
> (Guice.createInjector().getInstance(IMyClass.class)
> i get the Exception:
> Could not find a suitable constructor in MyClassImpl. Classes must have
> either one (and only one) constructor annotated with @Inject or a
> zero-argument constructor that is not private.
>   at MyClassImpl.class(MyClassImpl.java:??)
>   while locating IMyClass
>
> What do i do wrong?
> I know, i can use toInstance within the Module, but i wanted just to use
> Annotations. Is there a way to get this running?
>
> Regards,
> -Rainer

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to