I have an abstract base class that I use for services which includes the implementation of various methods, and a single abstract method. I have some closely related entities that I would like to manage within a single service, so I plan to have that service be a composition of anonymous implementations of the the abstract base service for those entities. I would like to inject the Dao implementation into the anonymous service implementation. I attempted to do something like this.
@Configurable
@Service( value="compositeService" )
public class CompositeServiceImpl
extends BaseDataServiceAbstract<AnotherEntity, Long>
implements CompositeService
{
BaseDataServiceAbstract<MyObject, Long> myObjectService =
new BaseDataServiceAbstract<MyObject, Long>() {
@Resource( name="myObjectDao")
BaseDao<MyObject, Long> myObjectDao;
@Override
public BaseDao<MyObject, Long> getDao()
{
return myObjectDao;
}
};
// other implementation methods and stuff
}
If I do it that way, the Dao does not get injected. If I move the dao out into the containing class, then it works fine. I'm ok with leaving it in the containing class, but wondering what it would take to inject it into the anonymous class implementation.
As long as you call new yourself, Spring doesn't instantiate the class and therefore won't be able to inject the dependencies.
And since you can't create anonymous classes without calling the default constructor yourself using new, there is no way Spring can help you here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With