Prev Next

Spring / Spring interview questions

How do I instantiate Spring bean by passing constructor-args programmatically?

You can use the ApplicationContext.getBean(String name, Object ... params) method, which allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

For example, the Employee object needs 2 constructor args (id and name) to instantiate.

@Component
public class Employee {

  private int id;
  private String name;


  public Employee(int id, String name) {
    this.id = id;
    this.name = name;
  }

}

To create the bean, use the below code.

Integer id = 102;
String name = "javapedia.net";
Employee emp = 
          (Employee)applicationContext.getBean("employee", id, name);

It's right time to invest in Cryptocurrencies! Earn free bitcoins up to $250 now by signing up.

Earn bitcoins upto $250 (free), invest in other Cryptocurrencies when you signup with blockfi. Use the referral link: Signup now and earn!

Using BlockFi, don't just buy crypto - start earning on it. Open an interest account with up to 8.6% APY, trade currencies, or borrow money without selling your assets.


Join CoinBase! We'll both receive $10 in free Bitcoin when they buy or sell their first $100 on Coinbase! Available in India also. Use the referral Join coinbase!


Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

Explain Spring Framework. What are the advantages of using Spring Framework? What are the different modules in Spring framework? Explain lifecycle of a bean in Spring framework. What is the difference between singleton and prototype bean? What are the types of Dependency Injection Spring supports? What is Bean Factory in Spring? How do I decide between when to use prototype scope and singleton-scoped bean? Different types of IOC. Difference between BeanFactory and ApplicationContext in spring. Type of transaction Management in spring. When do you use programmatic and declarative transaction management? What is Bean Wiring? Types of IoC containers. Explain Aspect oriented Programming. Explain different modes of bean autowiring in Spring Framework. How do I enable annotation based auto wiring in Spring Framework? What is Dependency Injection in Spring framework? Which DI should I prefer, Constructor-based or setter-based in spring? What is Spring annotation? Spring: What are Lazily-instantiated beans? Difference between the setter and constructor injection in Spring. Difference between Dependency Injection and Factory Pattern. What is the default scope of Spring bean? Mention the steps to create a Web application using Spring. How do you create bean for class with constructor having arguments? How does Spring achieve loose coupling? Can we use a final class as spring bean? When Spring creates a CGLIB proxy? When Spring creates a JDK dynamic proxy? Is Spring DTD/XSD mandatory for XML validation? Where I can find the Spring DTD? The main difference between JDK dynamic proxy and CGLib. How do I Inject value into static variables in Spring bean? Can we inject a eum to spring bean? Is spring prototype bean threadsafe? Is request scoped spring beans threadsafe? What is the preferred bean scope for DAO, Service and Controller? Difference between Java bean and spring bean. Mention an alternate DI framework like Spring. How to deploy a Spring Boot project to the Application Server (non-embedded)? What does the word "Actuator" in spring boot refer to? How do I instantiate Spring bean by passing constructor-args programmatically? What is the purpose of @ControllerAdvice Annotation?
Show more question and Answers...

Spring module questions

Comments & Discussions