How to inject ApplicationContext itself

JavaSpringApplicationcontext

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

Previous comments are ok, but I usually prefer:

@Autowired private ApplicationContext applicationContext;

Solution 2 - Java

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" />

Solution 3 - Java

Yes, just implement the ApplicationContextAware -interface.

Solution 4 - Java

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();
  }
}

Solution 5 - Java

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);
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmibutecView Question on Stackoverflow
Solution 1 - JavasinuhepopView Answer on Stackoverflow
Solution 2 - JavaJohan SjöbergView Answer on Stackoverflow
Solution 3 - JavaesajView Answer on Stackoverflow
Solution 4 - Javauser2587779View Answer on Stackoverflow
Solution 5 - JavaGrigory KislinView Answer on Stackoverflow