使对象弹簧管理

Joh*_*erg 2 java spring aspectj spring-aop autowired

如何管理已存在的对象弹簧?我想把它连接到Springs AoP使用的功能aspectj.我知道这是一个挑战,因为Spring AoP使用可能与对象一起创建的动态代理.

我为什么需要这个?

我有一个第三方类,它接受一个只在运行时知道的构造函数参数,因此我似乎无法将它添加到我的applicationContext或使用spring FactoryBean接口进行构造.还有其他方法吗?

我已经尝试了以下但没有取得巨大成功:

Obj obj = new ThirdPartyObj("runtime constructor arg");
appContext.getAutowireCapableBeanFactory().initializeBean(obj, "Obj");
Run Code Online (Sandbox Code Playgroud)

可能是弹簧管理的,但我仍然不能用它来触发方面.


[编辑] axtavt指出问题是我不使用从中返回的对象initializeBean(..).提到的两种方法都有效,但前提是:

  • 使用界面ObjInterface obj = (ObjInterface) ac.getBean("obj", args);或我们将获得:

    java.lang.ClassCastException: $Proxy28 cannot be cast to com.company.Obj

  • 不使用接口但启用CGLIB.这需要一个非私有的默认构造函数,否则我们将获得:

    java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

ska*_*man 5

为什么不创建一个包装的功能,一个新的类ThirdPartyObj,并认为 Spring管理.然后可以将依赖项注入其字段和方法参数中,并在实例化时传递ThirdPartyObj.