Make object spring managed

心不动则不痛 提交于 2019-12-06 12:11:23

You should be able to trigger aspects using this (note that you need to use returned object which can be a proxy):

Obj obj = new ThirdPartyObj("runtime constructor arg");     
obj = appContext.getAutowireCapableBeanFactory().initializeBean(obj, "Obj"); 

Another option is to declare it as a regular bean and pass the constructor argument via getBean():

Obj obj = appContext.getBean("Obj", "runtime constructor arg");     

Why not create a new class that wraps the functionality of ThirdPartyObj, and make that Spring-managed. Dependencies can then be injected into its fields and method parameters, and passed on the the instantiated ThirdPartyObj.

How about annotating the domain object with @Configurable annotation? I myself haven't tried it but seems like it might helpful in your scenario. AspectJ and Spring would create a managed object with attributes defined in the bean. The then created object instance can be used.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!