将 Spring Beans 注入非托管类中。

huangapple go评论84阅读模式
英文:

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&#39;m looking for some funcionality of spring like this

  public void someProcessingFunction(Class&lt;? extends APIClass&gt; 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,它提供了一些方便的方法,比如createBeanautowireBean

@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&lt;? extends APIClass&gt; 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&lt;? extends APIClass&gt; clazz) throws Exception {
    APIClass instance = clazz.getConstructor().newInstance();
    ctx.getAutowireCapableBeanFactory().autowireBean(instance);
    instance.executeBusinessStuffs();
  }
}

huangapple
  • 本文由 发表于 2020年8月19日 22:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定