Close

Spring - Injecting Prototype Bean Using Java 8 Functions

[Last Updated: Aug 11, 2020]

This example shows how to use Java 8 functions to solve narrower bean injection problem.

Example

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;

public class MyPrototypeBean {
  @Autowired
  private BeanNameFormatter beanNameFormatter;
  private String dateTimeString = LocalDateTime.now().toString();
  private String name;

  public MyPrototypeBean(String name) {
      this.name = name;
  }

  public String getDateTime() {
      return dateTimeString;
  }

  public String getFormattedName() {
      return beanNameFormatter.formatName(name);
  }
}
package com.logicbig.example;


public class BeanNameFormatter {
  public String formatName(String name) {
      return String.format("Formatted Bean Name: %s%n", name);
  }
}
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.function.Function;

public class MySingletonBean {

  @Autowired
  private Function<String, MyPrototypeBean> myPrototypeBeanFunction;

  public void showMessage() {
      MyPrototypeBean bean = myPrototypeBeanFunction.apply("Default Bean");
      System.out.printf("Time: %s from bean: %s - instance: %s%n", bean.getDateTime(), bean.getFormattedName(),
              System.identityHashCode(bean));
  }
}
package com.logicbig.example;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import java.util.function.Function;

@Configuration
public class AppConfig {

  @Bean
  @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  public MyPrototypeBean prototypeBean(String name) {
      return new MyPrototypeBean(name);
  }

  @Bean
  public Function<String, MyPrototypeBean> prototypeBeanFunction(){
      return name -> prototypeBean(name);
  }

  @Bean
  public MySingletonBean singletonBean() {
      return new MySingletonBean();
  }

  @Bean
  public BeanNameFormatter beanNameFormatterBean() {
      return new BeanNameFormatter();
  }


  public static void main(String[] args) throws InterruptedException {
      AnnotationConfigApplicationContext context =
              new AnnotationConfigApplicationContext(AppConfig.class);
      MySingletonBean bean = context.getBean(MySingletonBean.class);
      bean.showMessage();
      Thread.sleep(1000);

      bean = context.getBean(MySingletonBean.class);
      bean.showMessage();
  }
}
Time: 2020-06-05T00:35:41.787 from bean: Formatted Bean Name: Default Bean
- instance: 741669172
Time: 2020-06-05T00:35:42.795 from bean: Formatted Bean Name: Default Bean
- instance: 315860201

Example Project

Dependencies and Technologies Used:

  • spring-context 5.2.6.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.5.4

Using Java 8 Functions to solve Incompatible Bean Scopes problem Select All Download
  • spring-injecting-prototype-bean-using-java-8-functions
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MySingletonBean.java

    See Also