Backend Development 9 min read

Ways to Register Beans into the Spring Container

This article explains various methods to add beans to the Spring IoC container, including @Configuration with @Bean, component scanning, @Import with selectors or registrars, FactoryBean usage, and BeanDefinitionRegistryPostProcessor, providing code examples and detailed explanations for each approach.

Top Architect
Top Architect
Top Architect
Ways to Register Beans into the Spring Container

In Spring development, beans must be managed by the container. This article summarizes several ways to register a bean.

@Configuration + @Bean

Define a @Configuration class and declare a @Bean method returning the bean instance.

@Configuration
public class MyConfiguration {
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("spring");
        return person;
    }
}

@Component + @ComponentScan

Annotate the class with @Component and enable scanning via @ComponentScan on a configuration class.

@Component
public class Person {
    private String name;
    // getters and setters
}

@ComponentScan(basePackages = "com.springboot.initbean.*")
public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = ctx.getBean(Person.class);
        System.out.println(bean);
    }
}

@Import variations

Three forms: direct class import, ImportSelector, ImportBeanDefinitionRegistrar, and DeferredImportSelector, each allowing custom logic to add beans.

@Import(Person.class)
public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = ctx.getBean(Person.class);
        System.out.println(bean);
    }
}

FactoryBean

Implement FactoryBean to create bean instances, then register the factory as a @Bean.

class PersonFactoryBean implements FactoryBean
{
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }
    @Override
    public Class
getObjectType() {
        return Person.class;
    }
}

@Configuration
public class Demo1 {
    @Bean
    public PersonFactoryBean personFactoryBean() {
        return new PersonFactoryBean();
    }
    // main method omitted for brevity
}

BeanDefinitionRegistryPostProcessor

Implement this post‑processor to programmatically register BeanDefinition objects before the container refreshes.

public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
        registry.registerBeanDefinition("person", beanDefinition);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // no additional processing
    }
}

public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.addBeanFactoryPostProcessor(new MyBeanDefinitionRegistryPostProcessor());
        ctx.refresh();
        Person bean = ctx.getBean(Person.class);
        System.out.println(bean);
    }
}

The article lists five main techniques for adding beans to the Spring container, each accompanied by code examples and explanations.

JavaIoCSpringdependency injectionBean Registration
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.