This has probably been answered before but I can't...
# box-products
b
This has probably been answered before but I can't seem to find a solution. I have a parent and child component. Both should be able to be used independently. Each one has their own dependencies declared using the
init
method. I was hoping to be able to
extend
the child component from the parent and just call the methods directly but the dependencies in the child component don't seem to get injected. I know the simple workaround is to just inject the child component instead but is there any way to use inheritance instead?
b
@Brandon Brown I'm just guessing, but since you mentioned "init()" I think I know what your problem is
init() is always called prior to autowiring happening.
I recommend all your injectins be cfproperty injections and any constructor logic you need to run that is using injected dependencies can go in an
onDIComplete()
method which won't be run until all the injections are finished
If this doesn't fix it, then please post some code and maybe we can find the issue
b
I assume the property injection would work. There are drawbacks to using the property injection though right? I thought I read something before that said the constructor method was preferred
b
Not that I know of
I've always used property injection for years and it's the "only way" as far as I'm concerned šŸ™‚
ā˜šŸ¼ 1
It's also the only method that supports circular injection
Constructor injection also creates more boilerplate in your code
Because you have to • have the init method • declare the args and put the annotations on them • store the passed dependencies
With property injection, it's just
Copy code
property name="thing-I-want" inject;
and you're done šŸ™‚
ā˜šŸ¼ 1
b
I agree that the constructor injection is a bit verbose.
Property injection did work
thanks
šŸ‘ 1
b
I've rarely run into that issue, but when I have, I just used the
threadSafe
annotation
You only really run into that if you are doing a bunch of work in your onDIComplete methods
The risk that page discusses is basically an object getting asked to do something before it's fully done being built.
r
I'm a proponent of everything that Brad mentioned above. That being said, fwiw, I believe if you were to use the constructor method with
init()
, then you might be able to make it work with inheritance by including
super.init()
into your component's
init()
that is doing the inheritance. This should call the base component's
init()
within the
init()
essentially. I believe that would allow for the base component dependencies to be utilized in the methods of "inheritor" component.