Hi, Actually, iPOJO is calling another constructor (copying yours with some code before) generated during the manipulation. Only objects created with this constructor are managed by iPOJO.
If you really want a static creation policy, you can use factory methods: http://felix.apache.org/site/constructing-pojo-objects-with-factory-methods.html. However notice that in this case, the iPOJO management starts once the object is returned from the method (so fields are not injected before). Regards, Clement On 11.10.2011, at 08:56, Benoît Thiébault wrote: > Hi list, > > I confirm what I told you yesterday evening: the EventBuilder > constructor is private. > > But you are right, iPOJO seems to be calling it anyway. It changed it to > look like this: > > /** > * Default constructor. > */ > private EventBuilder() { > System.out.println("Instance created"); > } > > When I run my sample bundle, the "Instance created" message appears > twice. > > I thus have two instances of my "singleton", in the one used the > dispatcher field has not been injected. > > I know that the singleton pattern is an anti-pattern and if I could > implement my helper otherwise, I would. But I don't see any other > solution. > > Kind regards > > Ben > > Le lundi 10 octobre 2011 à 21:01 +0200, Benoît Thiébault a écrit : >> You are right, I simplified the class for the mailing list. But in the real >> class, I have a private default constructor. I'm not at work anymore but I >> will check as soon as possible tomorrow morning.Andreas Prieß >> <[email protected]> a écrit :Hi, >> >> the version of EventBuilder you posted is not really a singleton, it has >> a public default constructor. So to me it seems you will end up with two >> instances of EventBuilder: >> >> One created by iPOJO and injected with an EventDispatcher and the one >> that will be created implicitly by the line: >> >> private static final EventBuilder INSTANCE = new EventBuilder(); >> >> So when you use the field INSTANCE in your code, that is _not_ the >> instance that was injected with EventDispatcher. Therefore you still get >> a NPException. >> >> >> HTH, >> >> Andreas >> >> >> On 10.10.2011 14:57, Benoît Thiébault wrote: >>> I replaced the line: >>> private EventDispatcher dispatcher = null; >>> by: >>> private EventDispatcher dispatcher; >>> >>> I still have a null pointer exception. Is it possible that this >>> behaviour due to the fact that the class construction is done when I >>> first call one of its static methods (in particular the declaration >>> private static final EventBuilder INSTANCE = new EventBuilder();) ? >> >>>>>> The implementation looks like this (this is a simplified version of it): >>>>>> >>>>>> public final class EventBuilder { >>>>>> /** Unique instance of the builder */ >>>>>> private static final EventBuilder INSTANCE = new EventBuilder(); >>>>>> /** Event to trigger. */ >>>>>> private static Event eventToTrigger; >>>>>> /** Reference to the dispatcher to use. */ >>>>>> private EventDispatcher dispatcher = null; >>>>>> /** Lock to prevent concurrent modifications. */ >>>>>> private static final Lock LOCK = new ReentrantLock(); >>>>>> >>>>>> public static EventBuilder event(final EventKey key) { >>>>>> LOCK.lock(); >>>>>> eventToTrigger = new DefaultEvent(key); >>>>>> return INSTANCE; >>>>>> } >>>>>> >>>>>> public void triggerCallEvent() { >>>>>> try { >>>>>> dispatcher.triggerCallEvent(eventToTrigger); >>>>>> } finally { >>>>>> LOCK.unlock(); >>>>>> } >>>>>> } >>>>>> } >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] >

