Java and Spring Level 2 This level · 49 of 84
Medium java spring

Difference between a @Configuration class and a normal class?

The 30 second answer

By default, Spring enhances a @Configuration class with CGLIB so calls between its @Bean methods go through the container. For singleton beans, that means reusing the managed instance rather than creating another object. Ordinary @Bean methods on a component don't have that interception; calling them directly is just a Java method call. Setting proxyBeanMethods to false disables interception, as Spring Boot auto-configurations do, so I inject dependencies through method parameters instead.

Could you say this in an interview?

Example:

@Configuration
class AppConfig {
    @Bean
    Clock clock() { return Clock.systemUTC(); }

    @Bean
    OrderService orderService() {
        return new OrderService(clock());   // which Clock?
    }
}

With @Configuration, clock() inside orderService() returns the same singleton Clock bean. With @Component instead (lite mode), it creates a second Clock that the container does not manage.

How the interception works. ConfigurationClassEnhancer creates a CGLIB subclass. Its BeanMethodInterceptor checks whether the container itself is calling the method to create the bean, or whether your code is calling it:

if (isCurrentlyInvokedFactoryMethod(beanMethod)) {
    // The factory is calling the bean method in order to instantiate and register the bean
    // ... invoke the super implementation of the method to actually create the bean instance.
    return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);
}
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);

ConfigurationClassEnhancer.java L384-L401 (shortened)

If it is your code, resolveBeanReference does a getBean(...) and returns the existing bean.

The flag: proxyBeanMethods defaults to true (Configuration.java L477). With false, no subclass is made, so the class can be final, startup is a bit faster, and it works better with GraalVM native images. Then you should inject dependencies as method parameters:

@Configuration(proxyBeanMethods = false)
class AppConfig {
    @Bean Clock clock() { return Clock.systemUTC(); }
    @Bean OrderService orderService(Clock clock) { return new OrderService(clock); }
}

Other differences:

  • A @Configuration class must not be final and its @Bean methods must not be private or final when proxying is on, because CGLIB must override them.
  • @Configuration is itself a @Component, so it is found by component scanning.
  • @Bean methods returning a BeanFactoryPostProcessor should be static, otherwise the configuration class is created too early (the source above logs a warning for this).

Alternate interview scenarios

"What is the difference between @Configuration and @Bean?"

The trap is treating them as alternatives. @Bean is method-level: it registers whatever that method returns as a bean, and it is how you register objects from classes you cannot annotate yourself, such as a Clock or a third-party client. @Configuration is class-level: it marks the class that holds those methods and switches on the proxying described above. So the real answer is the full mode versus lite mode difference: the same @Bean method behaves differently depending on whether the class around it is a proxied @Configuration.

Sources (2)
  1. Spring Framework reference: Basic concepts, full @Configuration vs. lite @Bean modedocs.spring.io
  2. @Configuration Javadoc (proxyBeanMethods)docs.spring.io
Previous
Next