Spring dependency injection into an anonymous implementation
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>() {
@Resour开发者_高级运维ce( 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.
To be honest with you, I don't think it's possible. For spring to inject the dao, it has to first instantiate that bean when the context loader starts up. It can't do that though because it's not aware of it. It would be similar to having a class that you instantiate in your constructor:because spring didn't instantiate it, it doesn't know about it. I think adding the dao to the containing service class is the only way you're going to get this to work. Alternatively, you could always just create concrete instances of those classes and add them to your context xml and have them injected into your service class. You could then inject the dao's into them, but I assume there's a reason you didn't do that in the first place.
You can't do this directly. However, there are 2 workarounds:
inject the Dao in the owning class, and in
@PostConsruct
invoke a setter on each of the anonymous classes, setting the dao (i.e. inject it manually)obtain the
ApplicationContext
(either implementApplicationContextAware
, or I think autowiring it would also work), and in@PostConstruct
callappContext.getAutowireCapableBeanFactory().autowireBean(myObjectService)
. This will tell spring to autowire the dependencies of these objects even though the objects themselves are not part of spring context.
But generally speaking, I don't think it's a good idea to create so much boilerplate code. There should be a way for a more optimal solution (I can't see because I don't have the whole picture)
精彩评论