如何注入ApplicationContext本身

发布于 2024-10-16 06:10:44 字数 202 浏览 7 评论 0原文

我想将 ApplicationContext 本身注入到一个 bean 中。

类似

public void setApplicationContext(ApplicationContect context) {
  this.context = context;
}

春天可能吗?

I want to inject an ApplicationContext itself to a bean.

Something like

public void setApplicationContext(ApplicationContect context) {
  this.context = context;
}

Is that possible in spring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

动次打次papapa 2024-10-23 06:10:44

以前的评论都可以,但我通常更喜欢:

@Autowired private ApplicationContext applicationContext;

Previous comments are ok, but I usually prefer:

@Autowired private ApplicationContext applicationContext;
淡水深流 2024-10-23 06:10:44

很简单,使用 ApplicationContextAware 接口。

public class A implements ApplicationContextAware {
  private ApplicationContext context;

  public void setApplicationContext(ApplicationContext context) {
      this.context = context;
  }
}

然后在您实际的 applicationContext 中您只需要引用您的 bean 即可。

<bean id="a" class="com.company.A" />

Easy, using the ApplicationContextAware interface.

public class A implements ApplicationContextAware {
  private ApplicationContext context;

  public void setApplicationContext(ApplicationContext context) {
      this.context = context;
  }
}

Then in your actual applicationContext you only need to reference your bean.

<bean id="a" class="com.company.A" />
莫多说 2024-10-23 06:10:44

Yes, just implement the ApplicationContextAware -interface.

蓦然回首 2024-10-23 06:10:44

我在上面看到一些关于 @Autowired 仍然无法工作的评论。以下内容可能会有所帮助。

这是行不通的:

@Route(value = "content", layout = MainView.class)
public class MyLayout extends VerticalLayout implements RouterLayout {

  @Autowired private ApplicationContext context;

   public MyLayout() {
    comps.add(context.getBean(MyComponentA.class)); // context always null :(
}

你必须这样做:

 @Autowired
  public MyLayout(ApplicationContext context) {
    comps.add(context.getBean(MyComponentA.class)); //context is set :)
}

或这样:


@PostConstruct
    private void init() {
    comps.add(context.getBean(MyComponentA.class)); // context is set :)
}

另请注意,Upload 是必须在 @PostConstruct 范围内设置的另一个组件。这对我来说是一场噩梦。希望这有帮助!

我差点忘了提到 @Scope 注释对于您的 Bean 可能是必需的,如下所示。在 Bean 中使用 Upload 时就会出现这种情况,因为 UI 在创建 Bean 之前不会实例化/附加,并且会导致抛出空引用异常。使用 @Route 时不会这样做,但使用 @Component 时会这样做 - 所以后者不是一个选项,如果 @Route 不可行,那么我建议使用 @Configuration 类来创建具有原型范围的 bean。

@Configuration
public class MyConfig {
  @Bean
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public MyComponentA getMyBean() {
    return new MyComponentA();
  }
}

I saw some comments above about @Autowired not working still. The following may help.

This will not work:

@Route(value = "content", layout = MainView.class)
public class MyLayout extends VerticalLayout implements RouterLayout {

  @Autowired private ApplicationContext context;

   public MyLayout() {
    comps.add(context.getBean(MyComponentA.class)); // context always null :(
}

You must do this:

 @Autowired
  public MyLayout(ApplicationContext context) {
    comps.add(context.getBean(MyComponentA.class)); //context is set :)
}

or this:


@PostConstruct
    private void init() {
    comps.add(context.getBean(MyComponentA.class)); // context is set :)
}

Also note that Upload is another component that must be set within the scope of @PostConstruct. This was a nightmare for me to figure out. Hope this helps!

I almost forgot to mention that the @Scope annotation may be necessary for your Bean, as seen below. This was the case when using Upload within a Bean because the UI is not instantiate/attached prior to the Bean being created and will cause a Null Reference Exception to be thrown. It won't do so when using @Route, but will when using @Component - so the latter is not an option and if @Route is not viable, then I would recommend using @Configuration class to create the bean with the prototype scope.

@Configuration
public class MyConfig {
  @Bean
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public MyComponentA getMyBean() {
    return new MyComponentA();
  }
}
揪着可爱 2024-10-23 06:10:44

特殊解决方案:从任何(非 Spring)类获取 Spring beans

@Component
public class SpringContext {
    private static ApplicationContext applicationContext;

    @Autowired
    private void setApplicationContext(ApplicationContext ctx) {
        applicationContext = ctx;
    }

    public static <T> T getBean(Class<T> componentClass) {
        return applicationContext.getBean(componentClass);
    }
}

Special solution: get Spring beans from any (non Spring) classes

@Component
public class SpringContext {
    private static ApplicationContext applicationContext;

    @Autowired
    private void setApplicationContext(ApplicationContext ctx) {
        applicationContext = ctx;
    }

    public static <T> T getBean(Class<T> componentClass) {
        return applicationContext.getBean(componentClass);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文