Spring - Static Factory Method

Spring - Static Factory Method

In Spring, apart from using constructors for bean instantiation, you can also make use of factory methods, including static factory methods, to create bean instances. When using a static factory method, the method itself is responsible for returning the instance of the bean.

Here's a step-by-step example of how to use a static factory method in Spring:

  • Java Classes:

Let's assume we have a Car class, and we want to instantiate it using a static factory method:

public class Car { private String model; private Car(String model) { this.model = model; } public static Car createCarInstance(String model) { return new Car(model); } @Override public String toString() { return "Car [model=" + model + "]"; } } 

Notice that the constructor is private, and we provided a public static method createCarInstance that will be responsible for creating the instance of Car.

  • Spring XML Configuration:

To configure the bean using a static factory method, use the class attribute to specify the class containing the static method and the factory-method attribute to specify the name of the static method:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="Car" factory-method="createCarInstance"> <constructor-arg value="Sedan"/> </bean> </beans> 
  • Testing:

To ensure everything is correctly set up, let's test our configuration:

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Car car = (Car) context.getBean("car"); System.out.println(car); } } 

When running the App class, it should print:

Car [model=Sedan] 

This demonstrates that the Car bean was successfully instantiated using the static factory method defined in the Car class.

Examples

  1. Using static factory methods in Spring:

    • Description: Introduces the concept of using static factory methods for bean creation in the Spring framework.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance() { return new MyBean(); } } 
  2. Static factory method vs constructor in Spring:

    • Description: Compares the usage of static factory methods and constructors for creating beans in the Spring framework.
    • Code:
      public class MyBean { // Using a constructor public MyBean() { // Initialization logic } // Using a static factory method public static MyBean createInstance() { return new MyBean(); } } 
  3. Creating beans with static factory methods in Spring:

    • Description: Demonstrates how to create beans using static factory methods in the Spring framework.
    • Code:
      <!-- XML Configuration --> <beans> <bean id="myBean" class="com.example.MyBeanFactory" factory-method="createInstance"/> </beans> 
  4. Configuring static factory methods in Spring XML:

    • Description: Guides on configuring static factory methods in the Spring XML configuration.
    • Code:
      <beans> <bean id="myBean" class="com.example.MyBeanFactory" factory-method="createInstance"/> </beans> 
  5. Spring static factory method example:

    • Description: Provides a simple example of using a static factory method to create a bean in a Spring application.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance() { return new MyBean(); } } 
  6. Static factory methods in Spring JavaConfig:

    • Description: Illustrates the usage of static factory methods in Spring JavaConfig.
    • Code:
      @Configuration public class AppConfig { @Bean public MyBean myBean() { return MyBeanFactory.createInstance(); } } 
  7. Injecting dependencies using static factory methods in Spring:

    • Description: Demonstrates how to inject dependencies into beans created using static factory methods in the Spring framework.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(AnotherBean dependency) { return new MyBean(dependency); } } 
  8. Defining static factory methods in Spring beans:

    • Description: Guides on defining static factory methods within Spring beans.
    • Code:
      public class MyBean { // Using a static factory method public static MyBean createInstance() { return new MyBean(); } } 
  9. Spring static factory method vs instance factory method:

    • Description: Compares the usage of static factory methods and instance factory methods in the Spring framework.
    • Code:
      public class MyBeanFactory { // Using a static factory method public static MyBean createInstance() { return new MyBean(); } // Using an instance factory method public MyBean createInstanceUsingInstanceMethod() { return new MyBean(); } } 
  10. Customizing static factory method behavior in Spring:

    • Description: Demonstrates how to customize the behavior of static factory methods in the Spring framework.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(String customizationParam) { // Customization logic based on the parameter return new MyBean(customizationParam); } } 
  11. Handling dependencies in static factory methods with Spring:

    • Description: Guides on handling dependencies within static factory methods when creating beans in Spring.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(AnotherBean dependency) { return new MyBean(dependency); } } 
  12. Using static factory methods for bean creation in Spring Boot:

    • Description: Demonstrates how to use static factory methods for bean creation in a Spring Boot application.
    • Code:
      @SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @Bean public MyBean myBean() { return MyBeanFactory.createInstance(); } } 
  13. Configuring parameters for static factory methods in Spring:

    • Description: Guides on configuring parameters for static factory methods in Spring.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(String param1, int param2) { // Logic using parameters return new MyBean(param1, param2); } } 
  14. Lazy initialization with static factory methods in Spring:

    • Description: Shows how to achieve lazy initialization using static factory methods in the Spring framework.
    • Code:
      public class MyBeanFactory { private static MyBean instance; public static synchronized MyBean createInstance() { if (instance == null) { instance = new MyBean(); } return instance; } } 
  15. Injecting values into static factory methods in Spring:

    • Description: Demonstrates how to inject values into static factory methods when creating beans in Spring.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(String injectedValue) { // Logic using the injected value return new MyBean(injectedValue); } } 
  16. Static factory method with conditional bean creation in Spring:

    • Description: Shows how to conditionally create beans using static factory methods in the Spring framework.
    • Code:
      public class MyBeanFactory { public static MyBean createInstance(boolean condition) { if (condition) { return new MyBean(); } else { return null; // or handle differently based on condition } } } 

More Tags

sqldataadapter uiscreen entity-framework-core-2.1 cloud9-ide activemq-classic celery meta-tags dllimport ssms-2017 warnings

More Programming Guides

Other Guides

More Programming Examples