Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bean using application context in spring boot

I am developing a SpringBoot project and I want to get the bean by its name using applicationContext. I have tried many solution from web but could not succeed. My Requirement is that I have a controller

ControllerA 

and inside the controller I have a method getBean(String className). I want to get instance of registered bean. I have hibernate entities and I want to get an instance of the bean by passing the name of class only in getBean method.

Please help if someone know the solution.

like image 439
Qasim Avatar asked Dec 04 '15 12:12

Qasim


People also ask

How do you get a bean in Spring boot application context?

Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.) In the Application , we create a bean, call its method and set up the Spring Boot application.

How would you access application context inside a Java Bean?

How to access ApplicationContext inside a java bean? The ApplicationContext implementation which we are using in our application will invoke this method and pass the concrete object for AppplicationContext. Using this we can get access to all the configuration information.

How do I get an application context object?

To get a reference to the ApplicationContext in a Spring application, it can easily be achieved by implementing the ApplicationContextAware interface. Spring will automatically detect this interface and inject a reference to the ApplicationContext: view rawMyBeanImpl. java hosted by GitHub.

Is application context a bean?

The interfaces BeanFactory and ApplicationContext represent the Spring IoC container. Here, BeanFactory is the root interface for accessing the Spring container. It provides basic functionalities for managing beans. On the other hand, the ApplicationContext is a sub-interface of the BeanFactory.


2 Answers

You can Autowire the ApplicationContext, either as a field

@Autowired private ApplicationContext context; 

or a method

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

Finally use

context.getBean(SomeClass.class) 
like image 89
reos Avatar answered Sep 30 '22 21:09

reos


You can use ApplicationContextAware.

ApplicationContextAware:

Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in. Implementing this interface makes sense for example when an object requires access to a set of collaborating beans.

There are a few methods for obtaining a reference to the application context. You can implement ApplicationContextAware as in the following example:

package hello;  import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;   @Component public class ApplicationContextProvider implements ApplicationContextAware {      private ApplicationContext applicationContext;      @Override     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {         this.applicationContext = applicationContext;     }    public ApplicationContext getContext() {         return applicationContext;     }      } 

Update:

When Spring instantiates beans, it looks for ApplicationContextAware implementations, If they are found, the setApplicationContext() methods will be invoked.

In this way, Spring is setting current applicationcontext.

Code snippet from Spring's source code:

private void invokeAwareInterfaces(Object bean) {         .....         .....  if (bean instanceof ApplicationContextAware) {                   ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);    } } 

Once you get the reference to Application context, you get fetch the bean whichever you want by using getBean().

like image 20
Sundararaj Govindasamy Avatar answered Sep 30 '22 23:09

Sundararaj Govindasamy