Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Spring beans are typically registered declaratively via annotations like @Component or @Bean. But in some cases, we need more flexibility that we only have with programmatic registration, e.g., registering beans based on properties.

Spring 7 introduces the BeanRegistrar as a powerful new feature.

In this tutorial, we’ll look at what makes BeanRegistrar such a welcome addition, how it compares to the old way of registering beans, and how we can use it in both Java and Kotlin projects.

2. Why We Need Something New

Before Spring 7, we could already register beans programmatically via the BeanDefinitionRegistryPostProcessor type:

@Configuration
public class BeanRegistrationsSpring6Configuration {
    @Bean
    BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor() {
        return new BeanDefinitionRegistryPostProcessor() {
            @Override
            public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
                var beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(MyService.class);
                beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
                registry.registerBeanDefinition("myService", beanDefinition);
            }

            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                // No-op
            }
        };
    }
}

While flexible, it has drawbacks:

  • It’s verbose and low-level.
  • It’s difficult to read and test.
  • It’s not friendly to native image (AOT) scenarios.

3. The New BeanRegistrar Type

BeanRegistrar is a functional interface that lets us register beans in a clean, declarative, and native-compatible way. The following example uses BeanRegistrar to register one bean of type MyService with default settings, and another bean named “myService2” with prototype scope and lazy initialization programmatically:

@Configuration
@Import(BeanRegistrationsConfiguration.MyBeanRegistrar.class)
public class BeanRegistrationsConfiguration {
    static class MyBeanRegistrar implements BeanRegistrar {
        @Override
        public void register(BeanRegistry registry, Environment env) {
            registry.registerBean(MyService.class);
            registry.registerBean("myService2", MyService.class, spec -> spec
                .prototype()
                .lazyInit()
                .primary()
            );
        }
    }
}

We need to be aware that we cannot use the @Bean-style for the BeanRegistrar. We should instead use @Import because the bean must be discovered during Spring’s configuration processing time.

4. Kotlin DSL Support

For Kotlin, we can use BeanRegistrarDsl to have first-class support. The same example would then look like this:

@Configuration
@Import(BeanRegistrationsConfiguration.MyBeanRegistrar::class)
class BeanRegistrationsConfiguration {
    class MyBeanRegistrar : BeanRegistrarDsl({ 
        registerBean<MyService>() 
        registerBean( name = "myService2", prototype = true, lazyInit = true ){
            MyService()
        }
    })
}

5. Conclusion

In this article, we’ve learned that BeanRegistrar is a feature that lets us register beans in a clean, declarative, and native-compatible way.

We can find the original discussion about this feature in the Spring Framework Project at GitHub.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW