Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A @Bean method that returns a PropertySourcesPlaceholderConfigurer causes a warning and @Value fields in the same configuration class are null. Why, and what is the fix?

PropertySourcesPlaceholderConfigurer is a BeanFactoryPostProcessor, so Spring must create it before any ordinary bean. If its @Bean method is not static, Spring has to create the configuration class first to call the method, and at that moment the BeanPostProcessors that fill @Value and @Autowired are not registered yet, so the configuration's @Value fields stay null. Declaring the @Bean method static fixes it, and Spring logs a warning that points to exactly this problem.

Why this question is asked

This bug catches people in projects that do not use Spring Boot, or that bring in custom post-processors, and it is confusing because @Value works in every other class. The warning in the log names the cause, but only if we know how to read it. Interviewers ask this to test whether we understand the order in which the container creates things. In this answer, we will recap the two kinds of post-processors, reproduce the problem, follow the startup order that causes it, apply the static fix, and see why the fix works.

1A Quick Recap: Two Kinds of Post-Processors

Spring has two extension points that are easy to confuse. A BeanFactoryPostProcessor runs early, after bean definitions are loaded and before any ordinary bean is created, and works on the definitions, the recipes for beans. PropertySourcesPlaceholderConfigurer is one: it walks the definitions and replaces ${...} placeholders, and it also supplies the resolver that @Value uses for placeholders.

A BeanPostProcessor runs later, around the creation of each ordinary bean. AutowiredAnnotationBeanPostProcessor is one: it is the component that actually fills fields annotated with @Value and @Autowired. So @Value needs both: the placeholder resolver, and the bean post-processor that injects the value into the field. The second only acts on beans created after it was registered.

2Reproducing the Problem

Here is a configuration class in a plain Spring application, without Spring Boot, which therefore declares its own placeholder configurer.

Java
@Configuration
@PropertySource("classpath:app.properties") // db.url=jdbc:postgresql://db/orders
public class AppConfig {
@Value("${db.url}")
private String dbUrl;
@Bean // not static
public PropertySourcesPlaceholderConfigurer placeholders() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource dataSource() {
System.out.println("url = " + dbUrl); // url = null
return new DriverManagerDataSource(dbUrl);
}
}

At startup, the log shows a message from ConfigurationClassEnhancer, at INFO or WARN level depending on the version, along these lines: @Bean method AppConfig.placeholders is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface, which will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class; add the static modifier to this method to avoid these container lifecycle issues. The exact wording may vary by version, but it describes our bug precisely.

3Why the Field Is Null: Following the Startup Order

During refresh, Spring invokes BeanFactoryPostProcessors before it registers BeanPostProcessors, and both before it creates ordinary singletons. To invoke our placeholder configurer, Spring must first create it, and to create it, it must call placeholders(). Because the method is an instance method, it needs an instance of AppConfig. So Spring creates the AppConfig bean right then, very early.

At that moment AutowiredAnnotationBeanPostProcessor has not been registered yet. AppConfig is therefore created without its @Value field being processed, and without @Autowired fields or @PostConstruct methods. The configurer works fine afterwards, but AppConfig is already built and is never processed again. Later, when Spring calls dataSource() on that same AppConfig instance, dbUrl is still null.

Step throughStartup order with a non-static method
  1. Definitions loadedAppConfig, placeholders, dataSource
  2. Invoke BeanFactoryPostProcessorsneeds the placeholders bean
  3. Instance method: create AppConfig firstno BeanPostProcessors yet, @Value skipped
  4. Call placeholders(), run the configurerplaceholders ready
  5. Register BeanPostProcessorstoo late for AppConfig
  6. Create dataSource on the same AppConfigdbUrl is null

6 steps in order. Play them, or step through them one at a time.

4The Fix: Declare the Method static

A static method belongs to the class, not to an instance, so Spring can call it without creating AppConfig. Spring creates the configurer from the static method, runs it, registers the BeanPostProcessors, and only then creates AppConfig as an ordinary bean, with full processing.

Java
@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig {
@Value("${db.url}")
private String dbUrl;
@Bean
public static PropertySourcesPlaceholderConfigurer placeholders() { // static
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource dataSource() {
System.out.println("url = " + dbUrl); // url = jdbc:postgresql://db/orders
return new DriverManagerDataSource(dbUrl);
}
}

The warning disappears and the value arrives. Notice one consequence of static: the static method is not intercepted by the CGLIB subclass of the configuration class, so calling it from another @Bean method would create a new instance. That never matters for post-processors, because nothing should call them directly.

The same rule applies to every BeanFactoryPostProcessor or BeanDefinitionRegistryPostProcessor declared with @Bean, including our own. BeanPostProcessor @Bean methods benefit from being static too, for a similar reason, since they are also created before ordinary beans.

5Other Fixes, and Why Spring Boot Users Rarely See It

Two other approaches avoid the problem. We can move the post-processor @Bean method into its own small configuration class that has no @Value or @Autowired fields, so creating that class early does no harm. Or we can avoid field injection in configuration classes altogether and take values as @Bean method parameters, for example dataSource(@Value("${db.url}") String url), which are resolved when the method is called, after all post-processors are ready.

Spring Boot registers a PropertySourcesPlaceholderConfigurer itself, through a static @Bean method in its auto-configuration, so Boot applications rarely declare one. The bug then shows up with other BeanFactoryPostProcessors teams write themselves, with the same warning in the log.

Java
@Bean
public DataSource dataSource(@Value("${db.url}") String url) { // resolved when called
return new DriverManagerDataSource(url); // jdbc:postgresql://db/orders
}

6Follow-up questions

Why does Spring not just process AppConfig again later?

Because a bean is created and initialised once. Spring would have to re-inject fields and rerun init callbacks on an object that may already have been used, which could have side effects. Instead it warns us and expects the configuration to avoid early creation, which a static method does cleanly.

Does the same problem affect @Autowired fields?

Yes. Any annotation handled by a BeanPostProcessor, @Autowired, @Value, @Resource, @PostConstruct, and also proxies such as @Transactional on that class, is skipped for a bean that is created before the post-processors are registered. The warning text names those annotations.

How do I know if a bean was created too early in general?

Spring logs an INFO message saying a bean is not eligible for getting processed by all BeanPostProcessors, for example not eligible for auto-proxying. Searching the startup log for that phrase is a quick way to find beans that a post-processor pulled in early, which often explains missing proxies or null fields.

7Conclusion

In this answer, we saw that PropertySourcesPlaceholderConfigurer is a BeanFactoryPostProcessor and must be created before ordinary beans. A non-static @Bean method forces Spring to create its configuration class early, before the BeanPostProcessors that handle @Value and @Autowired are registered, so the class's @Value fields stay null. Spring warns about non-static BeanFactoryPostProcessor bean methods, and the fix is to declare such methods static, or to keep injection out of the configuration class by using method parameters.

What to say in the interviewAbout 80 seconds aloud

Open with

The key fact is that PropertySourcesPlaceholderConfigurer is a BeanFactoryPostProcessor, and Spring runs those very early in refresh, before it registers the BeanPostProcessors and before it creates ordinary beans.

Then, if they want more

To run it, Spring must create it, which means calling the @Bean method. If the method is an instance method, Spring first has to create the configuration class itself, right then. At that point AutowiredAnnotationBeanPostProcessor, the thing that fills @Value and @Autowired fields, is not registered yet, so the configuration object is created without injection.

It is never processed again, and when its other @Bean methods run later, the @Value fields are still null. Spring actually tells us this: it logs a message that the @Bean method is non-static and returns a BeanFactoryPostProcessor, which will result in a failure to process annotations like @Autowired and @PostConstruct in that configuration class, and it suggests adding static.

The fix is exactly that: declare the method static, so Spring can call it without an instance, run the configurer, register the post-processors and only then create the configuration class normally. The same rule applies to any BeanFactoryPostProcessor @Bean method.

Alternatively, I avoid field injection in configuration classes and take values as @Bean method parameters, which are resolved when the method is called.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A @Bean method that returns a PropertySourcesPlaceholderConfigurer causes a warning and...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/96f10099ea/