5 Essential Ways to Register Beans in Spring’s IoC Container
This article explains five core techniques for adding beans to the Spring IoC container—including @Configuration with @Bean, @ComponentScan, various @Import strategies, FactoryBean, and BeanDefinitionRegistryPostProcessor—providing code examples and key considerations for each method.
How can a bean be placed into the Spring container? This guide summarizes the most common approaches.
1. @Configuration + @Bean
Declare a configuration class with @Configuration and define beans using @Bean.
@Configuration
public class MyConfiguration {
@Bean
public Person person() {
Person person = new Person();
person.setName("spring");
return person;
}
}2. @Component + @ComponentScan
Mark a class with @Component and enable scanning with @ComponentScan on a configuration class.
@Component
public class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override
public String toString() { return "Person{name='" + name + "'}"; }
}
@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);
}
}Output: Person{name='null'} – the bean is successfully managed by the IoC container.
3. @Import Annotation
The @Import annotation can import configuration classes or regular components, often combined with custom annotations.
3.1 Direct class import
public class Person { /* fields and methods */ }
@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);
}
}Note: The Person class itself does not need any Spring annotation.
3.2 @Import + ImportSelector
@Import(MyImportSelector.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);
}
}
class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.springboot.pojo.Person"};
}
}3.3 @Import + ImportBeanDefinitionRegistrar
@Import(MyImportBeanDefinitionRegistrar.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);
}
}
class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
registry.registerBeanDefinition("person", beanDefinition);
}
}3.4 @Import + DeferredImportSelector
@Import(MyDeferredImportSelector.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);
}
}
class MyDeferredImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{Person.class.getName()};
}
}4. Using FactoryBean Interface
Implement FactoryBean to create bean instances programmatically.
@Configuration
public class Demo1 {
@Bean
public PersonFactoryBean personFactoryBean() {
return new PersonFactoryBean();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = ctx.getBean(Person.class);
System.out.println(bean);
}
}
class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception { return new Person(); }
@Override
public Class<?> getObjectType() { return Person.class; }
}5. Using BeanDefinitionRegistryPostProcessor
Register a bean definition during container startup by implementing BeanDefinitionRegistryPostProcessor.
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
MyBeanDefinitionRegistryPostProcessor processor = new MyBeanDefinitionRegistryPostProcessor();
ctx.addBeanFactoryPostProcessor(processor);
ctx.refresh();
Person bean = ctx.getBean(Person.class);
System.out.println(bean);
}
}
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 {}
}Summary
@Configuration + @Bean
@ComponentScan + @Component
@Import with various selector interfaces
FactoryBean implementation
BeanDefinitionRegistryPostProcessor
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
