Thursday, November 15, 2018

Passing Arguments to getBean() Method in Spring

Sometimes in a Spring application when you get a bean from the application context you may also need to initialize the bean by calling it’s constructor with parameters. In this post we’ll see how to call the Spring bean’s non-default constructor by passing parameters.

BeanFactory interface getBean() method

In BeanFactory interface there are many overloaded getBean() methods out of which you can use the following variant which allows passing parameters tothe constructor.

  • getBean(java.lang.String name, java.lang.Object... args)- Return an instance, which may be shared or independent, of the specified bean. Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

Using this method to get the bean from the context you can also specify the constructor parameters. Note that ApplicationContext interface extends the BeanFactory interface so this method is available for use with in Context.

Spring example initializing bean with constructor parameters

Let’s say we have a Spring component MessagePrinter which has a constructor with one String parameter message.

public class MessagePrinter {
 private String message;
 public MessagePrinter(String message) {
  this.message = message;
 public void displayMessage() {
  System.out.println("Passed message is- " + message);
Another Spring component from which this bean is called.
public class TaskExecutorDemo implements ApplicationContextAware {
 private ApplicationContext applicationContext;
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  this.applicationContext = applicationContext;
 public void printMessages() {
  // Bean initialized with constructor params   
  MessagePrinter messagePrinter = applicationContext.getBean(
                    MessagePrinter.class, "Hello");

This class implements ApplicationContextAware interface so that it has reference to the ApplicationContext. Also, you can see when getBean() method is called to get an instance of MessagePrinter bean, String parameter is also passed to initialize it

Since Spring Java config is used so we need a Configuration class. That configuration is used just for passing packages for component scanning.
@ComponentScan(basePackages = "org.netjs.service")
public class AppConfig {


Java class to run the application

public class App {
    public static void main( String[] args ){
     AbstractApplicationContext context = new AnnotationConfigApplicationContext(
     TaskExecutorDemo ted = context.getBean(TaskExecutorDemo.class);


Passed message is- Hello

That's all for this topic Passing Arguments to getBean() Method in Spring. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. How to Inject Prototype Scoped Bean in Singleton Bean
  2. Spring Bean Life Cycle
  3. Lazy Initializing Spring Beans
  4. Wiring Collections in Spring
  5. Internationalization (i18n) Using MessageSource in Spring

You may also like-

  1. Spring Web MVC Tutorial
  2. Configuring DataSource in Spring Framework
  3. Autowiring Using Annotations in Spring
  4. Spring Web Reactive Framework - Spring WebFlux Tutorial
  5. Difference Between Array And ArrayList in Java
  6. Covariant Return Type in Java
  7. Multi-Catch Statement in Java Exception Handling
  8. Converting Numbers to Words - Java Program