Here is an example of what I like to have:
Let's assume I have a _given_ my.company.ConfigService (and I don't even have
the sources) which has NO default constructor:
public class ConfigService {
private int val1=0, val2=0;
public ConfigService(int ctParam) { val1=100*ctParam; }
public setVal2(int val2) { this.val2=val2; }
public getVal1...
}
And I like to have the ConfigService as singleton with name "cfgSvc" defined
via XML (since there is annotation in the given source).
The initialisation should be:
1.) create the ConfigService via it's constructor and ctParam=42
2.) call setVal2(666);
See what I mean? Is this even possible with the 'flat' structure in the samples
of the spec? This would not allow to additionally set a value for val2, or did
I confuse something?
How must the XML look like according to the spec?
txs and LieGrue,
strub
--- Mark Struberg <[email protected]> schrieb am Mi, 14.1.2009:
> Von: Mark Struberg <[email protected]>
> Betreff: Re: XML definition for constructor injection
> An: [email protected]
> Datum: Mittwoch, 14. Januar 2009, 11:32
> Hi Gurkan!
>
> > the immediate children of the XML bean declaration
> I also understood it this way when I first read the spec.
>
> from the spec:
> > For a simple bean defined using XML, the bean
> constructor may be
> > specified by listing the parameter types of the
> constructer, in
> > order, as direct children of the element that declares
> the bean.
>
> But on the other hand it was not really clear to me if the
> examples given in the spec are _with_ or _without_ the bean
> declaration itself (outer <myapp:TstBeanConstructor>
> in my example)!
>
> If we interpret it your way, then no 'mixed'
> scenario like the following would work:
>
> I like to have a @Named(value="config")
> @SessionScoped TstBeanConstructor PLUS I like to inject the
> value 7 in setI2.
>
> How can this be achieved if there is no separation between
> the constructor parameters and the additionally injected
> ones?
>
> Maybe this is not needed? Or it's a shortcoming of the
> spec? Or I'm completely confused? :)
>
> How I would write the XML (assuming a ct public
> TstBeanConstructor(int ctParam) { ... ):
>
> <myapp:TstBeanConstructor>
> <Named>config</Named>
> <SessionScoped/>
> <myapp:TstBeanConstructor>
> <myapp:ctParam>42</myapp:ctParam>
>
> </myapp:TstBeanConstructor>
> <myapp:i2>7</myapp:i1>
> </myapp:TstBeanConstructor>
>
> LieGrue,
> strub
>
>
> --- Gurkan Erdogdu <[email protected]> schrieb
> am Mi, 14.1.2009:
>
> > Von: Gurkan Erdogdu <[email protected]>
> > Betreff: Re: XML definition for constructor injection
> > An: [email protected]
> > Datum: Mittwoch, 14. Januar 2009, 8:14
> > Hi Mark;
> >
> > Your XML definition is not correct, I think.
> Specification
> > says that, the immediate children of the XML bean
> > declaration which correspond to *Java Type* is
> captured as
> > a *parameters* of this constructor. So the correct XML
> > definition may be written as;
> >
> > <myapp:TstBeanConstructor>
> > <RequestScoped/>
> > <lang: Integer/>
> > </myapp:TstBeanConstructor>
> >
> > where *lang* is the namespace that corresponds to the
> > *java.lang* package. You are able to give any
> namespace
> > instead of *lang*.
> >
> > When the container creates this bean instance, it will
> > inject the constructor parameter with looking the
> beans with
> > API type *Integer* and Binding Type *...@current*. If it
> finds
> > the bean successfully, it injects its current
> instance.
> >
> > For example, lets say that we define producer method
> in
> > other bean class (MyOtherBean) .
> >
> > class MyOtherBean
> > {
> >
> > @RequestScoped @Produces
> > public int getTstBeanConstructorInitialValue()
> > {
> > return 12;
> > }
> > }
> > Then, container calls this method via its parent bean
> > instance (MyOtherBean) and injects the resulted
> instance
> > (Integer:12) into the TstBeanConstructor.
> >
> > I did not try to execute this sample, but I think it
> works.
> >
> > Please look at the specification that contains full
> > explanation of the XML defined constructor
> declaration.
> >
> > /Gurkan
> >
> >
> >
> >
> > ________________________________
> > From: Mark Struberg <[email protected]>
> > To: [email protected]
> > Sent: Tuesday, January 13, 2009 10:51:43 PM
> > Subject: XML definition for constructor injection
> >
> > grr, my first mail got eaten by the spam daemon
> >
> >
> > --- Mark Struberg <[email protected]> schrieb am
> Di,
> > 13.1.2009:
> >
> > > Von: Mark Struberg <[email protected]>
> > > Betreff:
> > > An: [email protected]
> > > Datum: Dienstag, 13. Januar 2009, 21:46
> > > Hi!
> > >
> > > I like to test the constructor injection via XML
> > > definition.
> > >
> > > For what I understand after reading the spec, the
> > following
> > > should work:
> > >
> > > Assume we have the WebBean, _without_ any
> annotations:
> > >
> > > public class TstBeanConstructor
> > > {
> > >
> > > private int i1=0;
> > > private int i2=0;
> > >
> > > public TstBeanConstructor(int i1)
> > > {
> > > this.i1 = i1*100;
> > > }
> > >
> > > public int getI1()
> > > ...
> > >
> > >
> > > and the XML looks like:
> > >
> > > <myapp:TstBeanConstructor>
> > > <RequestScoped/>
> > > <myapp:TstBeanConstructor>
> > > <myapp:i1>12</myapp:i1>
>
> >
> > >
> > > </myapp:TstBeanConstructor>
> > > </myapp:TstBeanConstructor>
> > >
> > >
> > > should this work? At first I was a bit confused
> about
> > the 2
> > > <myapp:TstBeanConstructor>, is this correct
> in
> > the
> > > XML?
> > >
> > > txs and LieGrue,
> > > strub