Default constructor is not being used

View: New views
9 Messages — Rating Filter:   Alert me  

Default constructor is not being used

by pchacin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I have a class with two constructors: a default constructor with
no arguments and a constructor with three arguments. Sometimes
I want to use the default constructor, which uses default values, and
sometimes I want to use the other constructor, passing the
arguments from a properties file.

The problem is that picocontainer is always trying to use the constructor
with the three parameters, even is there is no matching arguments in the
configuration.


Here is how I'm defining my class:

    public MyAbstractClass implements MyInterface

        public MyAbstractClass(){

        }

    }

    public class MyClass extends MyAbstractClass

    public MyClass() {
       MyClass(DEFAULT_1,DEFAULT_2,DEFAULT_3);
    }

    public MyClass(String arg1,String arg2, int arg3){

    }

}


This is how I'm registering the component in the contaniner

        //Load properties from a file
        Properties configuration = loadFromFile();

        pico = new PicoBuilder(new PropertiesPicoContainer(configuration))
        .withConstructorInjection()
        .withBehaviors(new OptInCaching())
        .build();

     
         pico.as(Characteristics.SINGLE)
                 .as(Characteristics.USE_NAMES)
                 .addComponent(MyInterface.class,MyClass.class);

And this is how I'm creating an instance:
 
        MyClass instance = pico.getComponent(MyInterface.class);

Now, if the configuration file has the properties calles "arg1", "arg2"
and "arg3", the instance is created properly. However, if none of the
properties is in the file, the container still tries to use the same
constructor.

notice that in the properties file there are several other properties
for other components.

Thanks in advance

--
========================================================================
0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3            
U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN            
www:   http://personals.ac.upc.es/pchacin
========================================================================


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: Default constructor is not being used

by Michael Rimov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Pablo!

What you want is:

          pico.as(Characteristics.SINGLE)
                  .as(Characteristics.USE_NAMES)
                  .addComponent(MyInterface.class,MyClass.class,
Parameter.ZERO);
       
^^^^^^^^^^^^^^

A further explanation can be found about 1/2 way down the page here:

http://www.picocontainer.org/disambiguation.html

In particular, look under the heading: "Disambiguation using the Parameter
Objects".  

HTH!

                                                                        -Mike


> -----Original Message-----
> From: pchacin [mailto:pchacin@...]
> Sent: Wednesday, July 23, 2008 8:47 AM
> To: user@...
> Subject: [picocontainer-user] Default constructor is not being used
>
> Hi
>
> I have a class with two constructors: a default constructor with
> no arguments and a constructor with three arguments. Sometimes
> I want to use the default constructor, which uses default values, and
> sometimes I want to use the other constructor, passing the
> arguments from a properties file.
>
> The problem is that picocontainer is always trying to use the constructor
> with the three parameters, even is there is no matching arguments in the
> configuration.
>
>
> Here is how I'm defining my class:
>
>     public MyAbstractClass implements MyInterface
>
>         public MyAbstractClass(){
>
>         }
>
>     }
>
>     public class MyClass extends MyAbstractClass
>
>     public MyClass() {
>        MyClass(DEFAULT_1,DEFAULT_2,DEFAULT_3);
>     }
>
>     public MyClass(String arg1,String arg2, int arg3){
>
>     }
>
> }
>
>
> This is how I'm registering the component in the contaniner
>
>         //Load properties from a file
>         Properties configuration = loadFromFile();
>
>         pico = new PicoBuilder(new PropertiesPicoContainer(configuration))
>         .withConstructorInjection()
>         .withBehaviors(new OptInCaching())
>         .build();
>
>
>          pico.as(Characteristics.SINGLE)
>                  .as(Characteristics.USE_NAMES)
>                  .addComponent(MyInterface.class,MyClass.class);
>
> And this is how I'm creating an instance:
>
>         MyClass instance = pico.getComponent(MyInterface.class);
>
> Now, if the configuration file has the properties calles "arg1", "arg2"
> and "arg3", the instance is created properly. However, if none of the
> properties is in the file, the container still tries to use the same
> constructor.
>
> notice that in the properties file there are several other properties
> for other components.
>
> Thanks in advance
>
> --
> ========================================================================
> 0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
> 0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
> 0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3
> U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN
> www:   http://personals.ac.upc.es/pchacin
> ========================================================================
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Default constructor is not being used

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Better:-

          pico.as(SINGLE,  
USE_NAMES).addComponent(MyInterface.class,MyClass.class,  
Parameter.ZERO);


:-)

- Paul

On Jul 23, 2008, at 9:00 AM, Michael Rimov wrote:

> Hi Pablo!
>
> What you want is:
>
>          pico.as(Characteristics.SINGLE)
>                  .as(Characteristics.USE_NAMES)
>                  .addComponent(MyInterface.class,MyClass.class,
> Parameter.ZERO);
>
> ^^^^^^^^^^^^^^
>
> A further explanation can be found about 1/2 way down the page here:
>
> http://www.picocontainer.org/disambiguation.html
>
> In particular, look under the heading: "Disambiguation using the  
> Parameter
> Objects".
>
> HTH!
>
> -Mike
>
>
>> -----Original Message-----
>> From: pchacin [mailto:pchacin@...]
>> Sent: Wednesday, July 23, 2008 8:47 AM
>> To: user@...
>> Subject: [picocontainer-user] Default constructor is not being used
>>
>> Hi
>>
>> I have a class with two constructors: a default constructor with
>> no arguments and a constructor with three arguments. Sometimes
>> I want to use the default constructor, which uses default values, and
>> sometimes I want to use the other constructor, passing the
>> arguments from a properties file.
>>
>> The problem is that picocontainer is always trying to use the  
>> constructor
>> with the three parameters, even is there is no matching arguments  
>> in the
>> configuration.
>>
>>
>> Here is how I'm defining my class:
>>
>>    public MyAbstractClass implements MyInterface
>>
>>        public MyAbstractClass(){
>>
>>        }
>>
>>    }
>>
>>    public class MyClass extends MyAbstractClass
>>
>>    public MyClass() {
>>       MyClass(DEFAULT_1,DEFAULT_2,DEFAULT_3);
>>    }
>>
>>    public MyClass(String arg1,String arg2, int arg3){
>>
>>    }
>>
>> }
>>
>>
>> This is how I'm registering the component in the contaniner
>>
>>        //Load properties from a file
>>        Properties configuration = loadFromFile();
>>
>>        pico = new PicoBuilder(new  
>> PropertiesPicoContainer(configuration))
>>        .withConstructorInjection()
>>        .withBehaviors(new OptInCaching())
>>        .build();
>>
>>
>>         pico.as(Characteristics.SINGLE)
>>                 .as(Characteristics.USE_NAMES)
>>                 .addComponent(MyInterface.class,MyClass.class);
>>
>> And this is how I'm creating an instance:
>>
>>        MyClass instance = pico.getComponent(MyInterface.class);
>>
>> Now, if the configuration file has the properties calles "arg1",  
>> "arg2"
>> and "arg3", the instance is created properly. However, if none of the
>> properties is in the file, the container still tries to use the same
>> constructor.
>>
>> notice that in the properties file there are several other properties
>> for other components.
>>
>> Thanks in advance
>>
>> --
>> =
>> =
>> =
>> =====================================================================
>> 0 0 0  Pablo Chacin               | Dept. d'Arquitectura de  
>> Computadors
>> 0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
>> 0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3
>> U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN
>> www:   http://personals.ac.upc.es/pchacin
>> =
>> =
>> =
>> =====================================================================
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Default constructor is not being used

by pchacin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paul, Michael

Thanks for the advise. However, that's precisely what I DON'T WANT to do
because
then picocontainer would pick ALWAYS the constructor with no parameters.

I would like that picocontainer were able to pick the right constructor
depending on
if it finds the named parameters in the config or not. What I can't
understand is
why it tries to figure out the arguments  from the config if there are
not matching
names (in the example, no "arg1", "arg2" or "arg3")

Pablo

Paul Hammant wrote:

> Better:-
>
>          pico.as(SINGLE,
> USE_NAMES).addComponent(MyInterface.class,MyClass.class, Parameter.ZERO);
>
>
> :-)
>
> - Paul
>
> On Jul 23, 2008, at 9:00 AM, Michael Rimov wrote:
>
>> Hi Pablo!
>>
>> What you want is:
>>
>>          pico.as(Characteristics.SINGLE)
>>                  .as(Characteristics.USE_NAMES)
>>                  .addComponent(MyInterface.class,MyClass.class,
>> Parameter.ZERO);
>>    
>> ^^^^^^^^^^^^^^
>>
>> A further explanation can be found about 1/2 way down the page here:
>>
>> http://www.picocontainer.org/disambiguation.html
>>
>> In particular, look under the heading: "Disambiguation using the
>> Parameter
>> Objects".
>>
>> HTH!
>>
>>                                     -Mike
>>
>>
>>> -----Original Message-----
>>> From: pchacin [mailto:pchacin@...]
>>> Sent: Wednesday, July 23, 2008 8:47 AM
>>> To: user@...
>>> Subject: [picocontainer-user] Default constructor is not being used
>>>
>>> Hi
>>>
>>> I have a class with two constructors: a default constructor with
>>> no arguments and a constructor with three arguments. Sometimes
>>> I want to use the default constructor, which uses default values, and
>>> sometimes I want to use the other constructor, passing the
>>> arguments from a properties file.
>>>
>>> The problem is that picocontainer is always trying to use the
>>> constructor
>>> with the three parameters, even is there is no matching arguments in
>>> the
>>> configuration.
>>>
>>>
>>> Here is how I'm defining my class:
>>>
>>>    public MyAbstractClass implements MyInterface
>>>
>>>        public MyAbstractClass(){
>>>
>>>        }
>>>
>>>    }
>>>
>>>    public class MyClass extends MyAbstractClass
>>>
>>>    public MyClass() {
>>>       MyClass(DEFAULT_1,DEFAULT_2,DEFAULT_3);
>>>    }
>>>
>>>    public MyClass(String arg1,String arg2, int arg3){
>>>
>>>    }
>>>
>>> }
>>>
>>>
>>> This is how I'm registering the component in the contaniner
>>>
>>>        //Load properties from a file
>>>        Properties configuration = loadFromFile();
>>>
>>>        pico = new PicoBuilder(new
>>> PropertiesPicoContainer(configuration))
>>>        .withConstructorInjection()
>>>        .withBehaviors(new OptInCaching())
>>>        .build();
>>>
>>>
>>>         pico.as(Characteristics.SINGLE)
>>>                 .as(Characteristics.USE_NAMES)
>>>                 .addComponent(MyInterface.class,MyClass.class);
>>>
>>> And this is how I'm creating an instance:
>>>
>>>        MyClass instance = pico.getComponent(MyInterface.class);
>>>
>>> Now, if the configuration file has the properties calles "arg1", "arg2"
>>> and "arg3", the instance is created properly. However, if none of the
>>> properties is in the file, the container still tries to use the same
>>> constructor.
>>>
>>> notice that in the properties file there are several other properties
>>> for other components.
>>>
>>> Thanks in advance
>>>
>>> --
>>> ========================================================================
>>>
>>> 0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
>>> 0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
>>> 0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3
>>> U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN
>>> www:   http://personals.ac.upc.es/pchacin
>>> ========================================================================
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>


--
========================================================================
0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3            
U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN            
www:   http://personals.ac.upc.es/pchacin
========================================================================


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: Default constructor is not being used

by Michael Rimov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Thanks for the advise. However, that's precisely what I DON'T WANT to do
> because
> then picocontainer would pick ALWAYS the constructor with no parameters.
>
> I would like that picocontainer were able to pick the right constructor
> depending on
> if it finds the named parameters in the config or not. What I can't
> understand is
> why it tries to figure out the arguments  from the config if there are
> not matching
> names (in the example, no "arg1", "arg2" or "arg3")

Whoops... sorry. I haven't worked with named parameters:  *>punt<*  Paul?????


>
> Pablo
>
> Paul Hammant wrote:
> > Better:-
> >
> >          pico.as(SINGLE,
> > USE_NAMES).addComponent(MyInterface.class,MyClass.class, Parameter.ZERO);
> >
> >
> > :-)
> >
> > - Paul
> >
> > On Jul 23, 2008, at 9:00 AM, Michael Rimov wrote:
> >
> >> Hi Pablo!
> >>
> >> What you want is:
> >>
> >>          pico.as(Characteristics.SINGLE)
> >>                  .as(Characteristics.USE_NAMES)
> >>                  .addComponent(MyInterface.class,MyClass.class,
> >> Parameter.ZERO);
> >>
> >> ^^^^^^^^^^^^^^
> >>
> >> A further explanation can be found about 1/2 way down the page here:
> >>
> >> http://www.picocontainer.org/disambiguation.html
> >>
> >> In particular, look under the heading: "Disambiguation using the
> >> Parameter
> >> Objects".
> >>
> >> HTH!
> >>
> >>                                     -Mike
> >>
> >>
> >>> -----Original Message-----
> >>> From: pchacin [mailto:pchacin@...]
> >>> Sent: Wednesday, July 23, 2008 8:47 AM
> >>> To: user@...
> >>> Subject: [picocontainer-user] Default constructor is not being used
> >>>
> >>> Hi
> >>>
> >>> I have a class with two constructors: a default constructor with
> >>> no arguments and a constructor with three arguments. Sometimes
> >>> I want to use the default constructor, which uses default values, and
> >>> sometimes I want to use the other constructor, passing the
> >>> arguments from a properties file.
> >>>
> >>> The problem is that picocontainer is always trying to use the
> >>> constructor
> >>> with the three parameters, even is there is no matching arguments in
> >>> the
> >>> configuration.
> >>>
> >>>
> >>> Here is how I'm defining my class:
> >>>
> >>>    public MyAbstractClass implements MyInterface
> >>>
> >>>        public MyAbstractClass(){
> >>>
> >>>        }
> >>>
> >>>    }
> >>>
> >>>    public class MyClass extends MyAbstractClass
> >>>
> >>>    public MyClass() {
> >>>       MyClass(DEFAULT_1,DEFAULT_2,DEFAULT_3);
> >>>    }
> >>>
> >>>    public MyClass(String arg1,String arg2, int arg3){
> >>>
> >>>    }
> >>>
> >>> }
> >>>
> >>>
> >>> This is how I'm registering the component in the contaniner
> >>>
> >>>        //Load properties from a file
> >>>        Properties configuration = loadFromFile();
> >>>
> >>>        pico = new PicoBuilder(new
> >>> PropertiesPicoContainer(configuration))
> >>>        .withConstructorInjection()
> >>>        .withBehaviors(new OptInCaching())
> >>>        .build();
> >>>
> >>>
> >>>         pico.as(Characteristics.SINGLE)
> >>>                 .as(Characteristics.USE_NAMES)
> >>>                 .addComponent(MyInterface.class,MyClass.class);
> >>>
> >>> And this is how I'm creating an instance:
> >>>
> >>>        MyClass instance = pico.getComponent(MyInterface.class);
> >>>
> >>> Now, if the configuration file has the properties calles "arg1", "arg2"
> >>> and "arg3", the instance is created properly. However, if none of the
> >>> properties is in the file, the container still tries to use the same
> >>> constructor.
> >>>
> >>> notice that in the properties file there are several other properties
> >>> for other components.
> >>>
> >>> Thanks in advance
> >>>
> >>> --
> >>>
> ========================================================================
> >>>
> >>> 0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
> >>> 0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
> >>> 0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3
> >>> U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN
> >>> www:   http://personals.ac.upc.es/pchacin
> >>>
> ========================================================================
> >>>
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe from this list, please visit:
> >>>
> >>>    http://xircles.codehaus.org/manage_email
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >>    http://xircles.codehaus.org/manage_email
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
>
>
> --
> ========================================================================
> 0 0 0  Pablo Chacin               | Dept. d'Arquitectura de Computadors
> 0 0 0  e-mail: pchacin@... | UPC-Campus Nord. Modul D6-212
> 0 0 0  phone: +34 +93 405 4059    | Jordi Girona, 1-3
> U P C  fax:   +34 +93 401 7055    | 08034 Barcelona - SPAIN
> www:   http://personals.ac.upc.es/pchacin
> ========================================================================
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



SINGLE not returning a single instance

by pchacin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, it is me again :-D

Sorry for bombing the list today, but I'm facing some (apparently) non
obvious problems and can't find
the answers in the documentation and start to fell desperate.

Now, the problem is this with the SINGL option not working as expected
(at least, as expected by me)

I have a container defined as:

        //Load properties from a file
        Properties configuration = loadFromFile();

        pico = new PicoBuilder(new PropertiesPicoContainer(configuration))
        .withConstructorInjection()
        .withBehaviors(new OptInCaching())
        .build();

I have some component registered as follows:

         pico.as(Characteristics.SINGLE)
                 .as(Characteristics.USE_NAMES)
                 .addComponent(MyInterface1.class,MyClass1.class);

         pico.as(Characteristics.SINGLE)
                 .as(Characteristics.USE_NAMES)
                 .addComponent(MyInterface2.class,MyClass2.class);

         pico.as(Characteristics.SINGLE)
                 .as(Characteristics.USE_NAMES)
                 .addComponent(MyInterface3.class,MyClass3.class);

         pico.as(Characteristics.SINGLE)
                 .as(Characteristics.USE_NAMES)
                 .addComponent(MyInterface4.class,MyClass4.class);


Classes MyClass2,  MyClass3 and MyClass4  all depend on a instance of
MyClass1. And I instantiate them as follows:

MyInterface2 = (MyInterface2)pico.getComponent(MyInterface2);
MyInterface3 = (MyInterface3)pico.getComponent(MyInterface3);
MyInterface4 = (MyInterface4)pico.getComponent(MyInterface4);
 
The problem is that each of them is getting a difference instance!

I double checked this, printing the stack trace on the constructor of
MyClass1 and it is called three times by picocontainer, one for each
of the getComponent statements listed above. I also checked and
all the calls are made in the same thread.

Am I missing something here?

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: SINGLE not returning a single instance

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

One of your problems is that you should on use as() once.

    pico.as(Characteristics.SINGLE,  
Characteristics.USE_NAMES).addComponent( ....

as() is not additive I think.  Try that quickly, but I promise to look  
again later today.


- Paul

On Jul 23, 2008, at 1:42 PM, pchacin wrote:

> Hi, it is me again :-D
>
> Sorry for bombing the list today, but I'm facing some (apparently) non
> obvious problems and can't find
> the answers in the documentation and start to fell desperate.
>
> Now, the problem is this with the SINGL option not working as expected
> (at least, as expected by me)
>
> I have a container defined as:
>
>        //Load properties from a file
>        Properties configuration = loadFromFile();
>
>        pico = new PicoBuilder(new  
> PropertiesPicoContainer(configuration))
>        .withConstructorInjection()
>        .withBehaviors(new OptInCaching())
>        .build();
>
> I have some component registered as follows:
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface1.class,MyClass1.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface2.class,MyClass2.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface3.class,MyClass3.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface4.class,MyClass4.class);
>
>
> Classes MyClass2,  MyClass3 and MyClass4  all depend on a instance of
> MyClass1. And I instantiate them as follows:
>
> MyInterface2 = (MyInterface2)pico.getComponent(MyInterface2);
> MyInterface3 = (MyInterface3)pico.getComponent(MyInterface3);
> MyInterface4 = (MyInterface4)pico.getComponent(MyInterface4);
>
> The problem is that each of them is getting a difference instance!
>
> I double checked this, printing the stack trace on the constructor of
> MyClass1 and it is called three times by picocontainer, one for each
> of the getComponent statements listed above. I also checked and
> all the calls are made in the same thread.
>
> Am I missing something here?
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: SINGLE not returning a single instance

by pchacin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That was the problem, now I'm getting the same instance each time.

Thanks


On Jul 23, 2008, at 10:20 PM, Paul Hammant wrote:

>One of your problems is that you should on use as() once.
>
>    pico.as(Characteristics.SINGLE,  
>Characteristics.USE_NAMES).addComponent( ....
>
>as() is not additive I think.  Try that quickly, but I promise to look  
>again later today.
>
>
>- Paul

On Jul 23, 2008, at 1:42 PM, pchacin wrote:

> Hi, it is me again :-D
>
> Sorry for bombing the list today, but I'm facing some (apparently) non
> obvious problems and can't find
> the answers in the documentation and start to fell desperate.
>
> Now, the problem is this with the SINGL option not working as expected
> (at least, as expected by me)
>
> I have a container defined as:
>
>        //Load properties from a file
>        Properties configuration = loadFromFile();
>
>        pico = new PicoBuilder(new  
> PropertiesPicoContainer(configuration))
>        .withConstructorInjection()
>        .withBehaviors(new OptInCaching())
>        .build();
>
> I have some component registered as follows:
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface1.class,MyClass1.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface2.class,MyClass2.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface3.class,MyClass3.class);
>
>         pico.as(Characteristics.SINGLE)
>                 .as(Characteristics.USE_NAMES)
>                 .addComponent(MyInterface4.class,MyClass4.class);
>
>
> Classes MyClass2,  MyClass3 and MyClass4  all depend on a instance of
> MyClass1. And I instantiate them as follows:
>
> MyInterface2 = (MyInterface2)pico.getComponent(MyInterface2);
> MyInterface3 = (MyInterface3)pico.getComponent(MyInterface3);
> MyInterface4 = (MyInterface4)pico.getComponent(MyInterface4);
>
> The problem is that each of them is getting a difference instance!
>
> I double checked this, printing the stack trace on the constructor of
> MyClass1 and it is called three times by picocontainer, one for each
> of the getComponent statements listed above. I also checked and
> all the calls are made in the same thread.
>
> Am I missing something here?
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: SINGLE not returning a single instance

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Cool - we're pleased that was it.

I've put a guard in the next version of PicoContainer such that it'll  
fail pretty clearly if you try to do as(X).as(Y) instead of as(X, Y).

- Paul

On Jul 24, 2008, at 1:30 AM, pchacin wrote:

> That was the problem, now I'm getting the same instance each time.
>
> Thanks
>
>
> On Jul 23, 2008, at 10:20 PM, Paul Hammant wrote:
>
>> One of your problems is that you should on use as() once.
>>
>>   pico.as(Characteristics.SINGLE,
>> Characteristics.USE_NAMES).addComponent( ....
>>
>> as() is not additive I think.  Try that quickly, but I promise to  
>> look
>> again later today.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email