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.
@Component @Scope("prototype") 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.
@Component public class TaskExecutorDemo implements ApplicationContextAware { private ApplicationContext applicationContext; @Override 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"); messagePrinter.displayMessage(); } }
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.@Configuration @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( AppConfig.class); TaskExecutorDemo ted = context.getBean(TaskExecutorDemo.class); ted.printMessages(); context.registerShutdownHook(); } }
Output
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
You may also like-
No comments:
Post a Comment