8 Proven Ways to Inject Prototype Beans into Singleton Beans in Spring Boot
This article explores eight practical strategies for injecting prototype‑scoped beans into singleton‑scoped beans in Spring Boot, detailing each method's implementation, advantages, and drawbacks, and provides code examples to help developers choose the most suitable approach.
1. Introduction
This article delves into various strategies for injecting a prototype‑scoped bean into a singleton‑scoped bean in Spring Boot 3.2.5, explaining the pros and cons of each method. By default, Spring creates beans as singletons, but when a prototype bean (new instance per request) is needed inside a singleton, special handling is required.
@Configuration
public class AppConfig {
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public PrototypeBean prototypeBean() {
return new PrototypeBean();
}
@Bean
public SingletonBean singletonBean() {
return new SingletonBean();
}
}The configuration defines PrototypeBean as prototype and SingletonBean as singleton.
@Component
public class SingletonBean {
@Resource
private PrototypeBean prototypeBean;
public void test() {
System.out.println(this.prototypeBean);
}
}Using @Resource injects the prototype bean, but calling test() repeatedly prints the same instance, demonstrating the scope‑injection problem.
2. Solutions
2.1 Implement ApplicationContextAware
public class SingletonBean implements ApplicationContextAware {
private ApplicationContext context;
public void test() {
System.out.println(this.context.getBean(PrototypeBean.class));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}Each call retrieves a fresh prototype from the context. Drawbacks: breaks inversion of control and tightly couples the class to Spring.
2.2 Use jakarta.inject.Provider
public class SingletonBean {
@Resource
private jakarta.inject.Provider<PrototypeBean> provider;
public void test() {
System.out.println(provider.get());
}
}The provider returns a new instance on every get() call.
2.3 Use Spring's ObjectProvider
public class SingletonBean {
@Resource
private ObjectProvider<PrototypeBean> objectProvider;
public void test() {
System.out.println(objectProvider.getIfAvailable());
}
} ObjectProvideroffers rich retrieval methods, including collection access.
2.4 Use Spring's ObjectFactory
public class SingletonBean {
@Resource
private ObjectFactory<PrototypeBean> objectFactory;
public void test() {
System.out.println(objectFactory.getObject());
}
} ObjectFactoryis a functional interface with a single getObject() method.
2.5 Use @Lookup annotation
public class SingletonBean {
public void test() {
System.out.println(get());
}
@Lookup
protected PrototypeBean get() {
return null; // Spring overrides to return a new prototype instance
}
}Spring creates a proxy for SingletonBean and overrides the get() method to return a fresh PrototypeBean each time.
2.6 Register a Function bean
@Bean
public <T> Function<Class<T>, T> targetBean(ApplicationContext context) {
return t -> context.getBean(t);
} public class SingletonBean {
@Resource
private Function<Class<?>, PrototypeBean> targetBean;
public void test() {
System.out.println(targetBean.apply(PrototypeBean.class));
}
}This generic function allows retrieving any prototype bean without hard‑coding the type.
2.7 Scope proxy
@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PrototypeBean {
}Configuring proxyMode=TARGET_CLASS makes Spring inject a proxy that always resolves to a new instance.
2.8 Use @Lazy annotation
public class SingletonBean {
@Resource
@Lazy
private PrototypeBean prototypeBean;
}The @Lazy proxy ensures the prototype bean is created only when first accessed, yielding a fresh instance per use.
These eight approaches provide flexible solutions for the prototype‑in‑singleton injection problem in Spring Boot applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
