Hello Fred,

thank you for your response. I solved the problem and deleted my post 
because i had an error within my source. I accidentally created another 
Injector within the servlet.

Now everything works fine.

Dirk



Am Sonntag, 16. Dezember 2012 00:41:20 UTC+1 schrieb Fred Faber:
>
>
>
> On Sat, Dec 15, 2012 at 3:41 PM, Dirk Nimerem 
> <[email protected]<javascript:>
> > wrote:
>
>> Hello,
>>
>> I'am developing an extension for the commercial Polarion ALM system. In 
>> my extension I'am using Google Guice for dependeny injection and I'am very 
>> satisfied with it. But I have one problem:
>>
>> I have an Interface ISingleton (just for demonstration) in my extension, 
>> which is specified as followed:
>>
>> @ImplementedBy(Singleton.class)
>> public interface ISingleton {
>> public int getUniqueNumber();
>> }
>>
>> My implementation for the Singleton (also just for demonstration) is the 
>> following: 
>>
>> @Singleton
>> public class Singleton implements ISingleton {
>> private final int uniquenumber = (int) (Math.random() * 10000);
>>
>> @Override
>> public int getUniqueNumber() {
>> return uniquenumber; 
>> }
>>  }
>>
>> Any classes which depends on the ISingleton get an instance to this via 
>> constructor injection. That works fine - in all my classes the Singleton 
>> hashcode is the same, also the unique number. At least for one part of my 
>> application- any SampleClass gets the same singleton:
>>
>> public class SampleClass {
>> private final ISingleton singleton;
>>
>> @Inject
>> public SampleClass(ISingleton singleton) {
>> this.singleton = singleton;
>> }
>> }
>>
>> But the problem is: My application consists of two parts: The first part 
>> is called from the Polarion application at Startup. I create my guice 
>> injector at this point. As mentioned above, that works fine, any class 
>> needing an ISingleton get the same Singleton instance.
>>
>> On the other hand my application also contains a servlet. If I initiate 
>> some SampleClass 
>> objects, all of them have another instance of the singleton. That means i 
>> have two instances of my singleton which is not acceptable for me.
>>
>> What can I do here? If i consult the injector created and temporarely 
>> stored at the startup within my servlet,
>
>
> I'm not familiar with the  Polarion ALM framework and so sorry for the 
> fundamental question, but how is the servlet created (through a different 
> injector, reflection, something else)?
>
> Another alternative in a pinch is to use a jvm-wide static instance of the 
> singleton, though it might not be ideal.
>
> Something like:
>
> class YourModule extends AbstractModule {
>   // Downside is that this doesn't use Guice to be created
>   private static final ISingleton singleton = new Singleton(...);
>  
>   @Override protected void configure() {
>     bind(ISingleton.class).toInstance(singleton);
>     ..
>   }
> }
>
> Fred
>
> i get the correct instance. But i want to use constructor injection and 
>> keeping a reference to the injector isn't a good idea. How can i solve it?
>>
>> Thank you in advance,
>> Dirk
>>
>> -- 
>> 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/-/0jsOnAocBpQJ.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> [email protected] <javascript:>.
>> For more options, visit this group at 
>> http://groups.google.com/group/google-guice?hl=en.
>>
>
>

-- 
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/-/HwT7rbgatMEJ.
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