英文:
Inject spring beans into a non-managed class
问题
我有这个非托管类,我想要注入Spring beans(我事先不知道它们是什么)。我该如何做?
例如,假设我有以下类:
public class NonManagedClass extends APIClass {
  @Resource
  private Service1 service;
  @Resource
  private Service2 service2;
  // 这里我可以声明许多不同的依赖
  @Resource
  private ServiceN serviceN;
  @Override
  public void executeBusinessStuffs() {
    // 业务逻辑
  }
}
我需要以某种方式让Spring在我的类中注入这些依赖项。我在创建后可以访问这些对象,所以对我来说很容易调用任何能够实现此功能的方法。例如:
@Service
public void SomeAPIService {
  @Resource
  private BeanInjector beanInjector; // 我正在寻找类似于Spring的这种功能
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = clazz.getConstructor().newInstance();
    beanInjector.injectBeans(instance);
    instance.executeBusinessStuffs();
  }
}
Spring是否具有根据字段注解来为非托管类注入bean的功能?
英文:
I have this non-managed class that I want to inject spring beans (that I don't known a-priory what they are). How can I do that?
For example, let's say I have the following class:
public class NonManagedClass extends APIClass {
  @Resource
  private Service1 service;
  @Resource
  private Service2 service2;
  // here i can declare many different dependencies
  @Resource
  private ServiceN serviceN;
  @Override
  public void executeBusinessStuffs() {
    // business logics
  }
}
I need in someway to let spring inject these dependencies in my class. I have access to these objects after created, so it's easy to me call any method that can accomplish this functionality. For example:
@Service
public void SomeAPIService {
  @Resource
  private BeanInjector beanInjector; // I'm looking for some funcionality of spring like this
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = clazz.getConstructor().newInstance();
    beanInjector.injectBeans(instance);
    instance.executeBusinessStuffs();
  }
}
Does Spring have such functionality to inject beans based on fields annotation for a non-managed class?
答案1
得分: 3
用ApplicationContext替换BeanInjector,你就快要完成了。从那里,你可以获取AutowireCapableBeanFactory,它提供了一些方便的方法,比如createBean和autowireBean。
@Service
public void SomeAPIService {
  @Resource
  private ApplicationContext ctx;
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = ctx.createBean(clazz);
    instance.executeBusinessStuffs();
  }
}
或者,如果你真的喜欢自己构建东西而不是使用容器:
@Service
public void SomeAPIService {
  @Resource
  private ApplicationContext ctx;
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = clazz.getConstructor().newInstance();
    ctx.getAutowireCapableBeanFactory().autowireBean(instance);
    instance.executeBusinessStuffs();
  }
}
英文:
Replace BeanInjector with ApplicationContext and you are almost there. From there you can get the AutowireCapableBeanFactory which provides some handy methods like createBean and autowireBean.
@Service
public void SomeAPIService {
  @Resource
  private ApplicationContext ctx;
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = ctx.createBean(clazz);
    instance.executeBusinessStuffs();
  }
}
or if you really like to construct stuff yourself instead of using the container:
@Service
public void SomeAPIService {
  @Resource
  private ApplicationContext ctx;
  public void someProcessingFunction(Class<? extends APIClass> clazz) throws Exception {
    APIClass instance = clazz.getConstructor().newInstance();
    ctx.getAutowireCapableBeanFactory().autowireBean(instance);
    instance.executeBusinessStuffs();
  }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论