Java spring手动添加bean

Java spring手动添加bean,java,spring,Java,Spring,我有一个使用Java1.4和Spring2.5的项目。 它使用一些第三方库,提供一些服务 class XFactory { Object getService(String name); } 其中,name是服务接口的名称,方法返回实现。 我想扩展这个类并将结果注册为SpringBean: class YFactory extends XFactory implements ApplicationContextAware { private ApplicationConte

我有一个使用Java1.4和Spring2.5的项目。 它使用一些第三方库,提供一些服务

class XFactory {
    Object getService(String name);
}
其中,
name
是服务接口的名称,方法返回实现。 我想扩展这个类并将结果注册为SpringBean:

class YFactory extends XFactory implements ApplicationContextAware {

     private ApplicationContext applicationContext;

     public void setApplicationContext(ApplicationContext applicationContext) throws       BeansException {
          this.applicationContext = applicationContext;
     }

     Object getService(String name) {
          Object service = super.getService(name);
          registerBean(applicationContext, name, service);
          return service;
     }

 }
如何实现注册bean?
我在谷歌上搜索,但N正在工作:将appCtx强制转换为DefaultListableBeanFactory会导致CCE,我无法将XFactory更改为FactoryBean(第三方)

'将appCtx强制转换为DefaultListableBeanFactory会导致CCE'
通过appCtx获取AutowireCapableBeanFactory。getAutowireCapableBeanFactory()并将其强制转换为BeanDefinitionRegistry。

“将appCtx强制转换为DefaultListableBeanFactory会导致CCE”
通过appCtx.getAutowireCapableBeanFactory()获取AutowireCapableBeanFactory,然后将其强制转换到BeanDefinitionRegistry。

添加了类似的内容:
ConfigurableBeanFactory cfb=(ConfigurableBeanFactory)applicationContext.getAutowireCapableBeanFactory()
如果(!cfb.containsBean(service.getName()){
cfb.registerSingleton(intfName,service);
}
不确定是否正常,但可能工作-appCtx.getBean(name)返回sth!=空,尚未完全测试。现在我有另一个问题。我使用SpringAOP,是否可以代理原始服务,使其包含我的AOP.xml中已定义的所有方面?“不确定是否正常”您的代码已经直接依赖于SpringAPI,因此如果将来对Spring进行任何更改,可能会破坏该部分(这不太可能)。这一举措不会产生太大影响。对于aop,我查看了代码,找不到答案。添加了如下内容:
ConfigurableBeanFactoryCfB=(ConfigurableBeanFactory)applicationContext.GetAutoWireCapableBeanFactory()
如果(!cfb.containsBean(service.getName()){
cfb.registerSingleton(intfName,service);
}
不确定是否正常,但可能工作-appCtx.getBean(name)返回sth!=空,尚未完全测试。现在我有另一个问题。我使用SpringAOP,是否可以代理原始服务,使其包含我的AOP.xml中已定义的所有方面?“不确定是否正常”您的代码已经直接依赖于SpringAPI,因此如果将来对Spring进行任何更改,可能会破坏该部分(这不太可能)。这一举措不会产生太大影响。对于aop,我查看了代码,找不到答案。