Spring Annotation-Based Bean Registration Guide
The guide explains how Spring’s annotation-driven configuration—using @Configuration and @Bean to define beans, @ComponentScan for automatic detection, and additional annotations such as @Scope, @Lazy, @Conditional, @Import and FactoryBean—replaces verbose XML, streamlining bean registration and lifecycle management.
This article introduces how to register components in Spring using annotations.
@Configuration and @Bean
Defines a configuration class and registers a bean via a @Bean method. Example bean class Dog and XML configuration are shown, then the annotation‑based equivalent.
public class Dog {
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override
public String toString() { return "Dog [name=" + name + ", age=" + age + "]"; }
} @Configuration
public class MyConfig {
@Bean
public Dog dog() {
Dog dog = new Dog();
dog.setName("Tom");
dog.setAge(5);
return dog;
}
}Using AnnotationConfigApplicationContext to obtain the bean.
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);
Dog dog = (Dog) ctx.getBean("tomDog");
System.out.println(dog);@ComponentScan
Scans packages to auto‑detect @Component , @Service , @Repository , @Controller . Example:
@Configuration
@ComponentScan("com.wwj")
public class MyConfig { }Shows includeFilters, excludeFilters, useDefaultFilters, custom TypeFilter , etc.
@ComponentScan(value = "com.wwj", excludeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = { Controller.class, Service.class })
})
public class MyConfig { }@Scope
Specifies bean scope (prototype, singleton, request, session).
@Scope("prototype")
@Bean
public Dog dog() {
Dog dog = new Dog();
dog.setName("Tom");
dog.setAge(5);
return dog;
}@Lazy
Enables lazy initialization of a bean.
@Lazy
@Bean
public Dog dog() {
Dog dog = new Dog();
dog.setName("Tom");
dog.setAge(5);
return dog;
}@Conditional
Registers a bean only when a custom condition matches.
@Conditional(MyCondition.class)
@Bean
public Dog dog() {
Dog dog = new Dog();
dog.setName("Tom");
dog.setAge(5);
return dog;
}@Import
Directly imports a class or selector into the context.
@Configuration
@Import(Dog.class)
public class MyConfig { } @Import(MyImportSelector.class)
public class MyConfig { }@FactoryBean
Creates beans via a FactoryBean implementation.
public class DogFactoryBean implements FactoryBean
{
@Override
public Dog getObject() throws Exception { return new Dog(); }
@Override
public Class
getObjectType() { return Dog.class; }
@Override
public boolean isSingleton() { return true; }
} @Configuration
public class MyConfig {
@Bean
public DogFactoryBean dogFactoryBean() {
return new DogFactoryBean();
}
}The article concludes that these annotations simplify bean registration compared with XML configuration.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.