让对象由 spring 管理
如何获得 spring 管理的现有对象?我想使用 aspectj
将其连接到 Springs AoP
功能。我知道这是一个挑战,因为 Spring AoP 使用可能与对象一起创建的动态代理。
为什么我需要这个?
我有一个第三方类,它采用一个仅在运行时中已知的构造函数参数, 因此,我似乎无法将其添加到我的 applicationContext
或使用 springs FactoryBean
接口进行构造。 还有其他方法吗?
我已经尝试过以下方法,但没有取得很大成功:
Obj obj = new ThirdPartyObj("runtime constructor arg");
appContext.getAutowireCapableBeanFactory().initializeBean(obj, "Obj");
它可能是由spring管理的,但我仍然无法使用它来触发方面。
[编辑] axtavt 指出问题是我没有使用从 initializeBean(..)
返回的对象。上述两种方法都有效,但前提是:
使用接口
ObjInterface obj = (ObjInterface) ac.getBean("obj", args);
否则我们将得到:java.lang.ClassCastException: $Proxy28 无法转换为 com.company.Obj
不使用接口但启用
CGLIB< /代码>。这需要一个非私有默认构造函数,否则我们将得到:
java.lang.IllegalArgumentException:超类没有 null 构造函数,但没有给出参数
How can I get an already existing object spring managed? I would like to hook it up to Springs AoP
capabilities using aspectj
. I know this to be a challenge since Spring AoP
uses dynamic proxies which probably are created along with the object.
Why do I need this?
I have a third-party class which takes a constructor argument which is only known in runtime,
hence it seems I cannot add it to my applicationContext
or use springs FactoryBean
interface for construction. Is there any other way?
I've already tried the following without great success:
Obj obj = new ThirdPartyObj("runtime constructor arg");
appContext.getAutowireCapableBeanFactory().initializeBean(obj, "Obj");
It might be spring-managed, but I still cannot use it to trigger aspects.
[EDIT] axtavt pointed out the problem is that I don't use the object returned from initializeBean(..)
. Both mentioned approaches work, but only if:
Using interface
ObjInterface obj = (ObjInterface) ac.getBean("obj", args);
or we will get a:java.lang.ClassCastException: $Proxy28 cannot be cast to com.company.Obj
Not using interface but enable
CGLIB
. This requires a non-private default constructor, or we will get a:java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不创建一个包装
ThirdPartyObj
功能的新类,并使其由 Spring 管理。然后可以将依赖项注入到其字段和方法参数中,并传递到实例化的ThirdPartyObj
上。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 instantiatedThirdPartyObj
.您应该能够使用它触发方面(请注意,您需要使用返回的对象,它可以是代理):
另一个选择是将其声明为常规 bean 并通过 getBean() 传递构造函数参数>:
You should be able to trigger aspects using this (note that you need to use returned object which can be a proxy):
Another option is to declare it as a regular bean and pass the constructor argument via
getBean()
:使用@Configurable注解来注解域对象怎么样?我自己还没有尝试过,但似乎它可能对您的场景有所帮助。 AspectJ 和 Spring 将创建一个托管对象,其属性在 bean 中定义。可以使用随后创建的对象实例。
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.