Hello Alen.
I tried again with my example, so this one works.
This is the Interface with the ProvidedBy-Annotation:
@ProvidedBy(MyClassImplProvider.class)
public interface IMyClass {
public boolean returnTrue();
}
This is the implementation:
@Singleton
public class MyClassImpl implements IMyClass {
private static MyClassImpl instance;
private MyClassImpl() {
}
@Singleton
public static MyClassImpl getInstance() {
if (instance == null) {
instance = new MyClassImpl();
}
return instance;
}
public boolean returnTrue() {
return true;
}
}
And this is the Provider:
public class MyClassImplProvider implements Provider<IMyClass> {
public MyClassImpl get() {
return MyClassImpl.getInstance();
}
}
Still, why can't the getInstance-method of MyClassImpl be annotated so that
it identifies, you can get your object here ..
Does someone else have an idea?
-Rainer
On Friday, July 6, 2012 3:53:11 PM UTC+2, Alen Vrečko wrote:
>
> 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 view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/-HqQvWUXNMYJ.
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.