Mastering Spring Bean Definitions: XML, Annotations, JavaConfig & Import Techniques
This comprehensive guide explores every way to define Spring beans—from classic XML configurations and constructor or setter injection to modern @Component annotations, JavaConfig @Bean methods, and advanced @Import, ImportSelector, and post‑processor techniques—empowering developers to choose the most suitable approach for any project.
Introduction
Spring plays a crucial role in the Java ecosystem as a bean factory, offering multiple ways to define beans ranging from classic XML configuration to modern annotation‑based and Java‑based approaches.
1. XML bean configuration
1.1 Constructor
Define a bean using
<bean id="personService" class="com.sue.cache.service.test7.PersonService">with optional <constructor-arg> elements to pass arguments.
<bean id="personService" class="com.sue.cache.service.test7.PersonService">
<constructor-arg index="0" value="susan"/>
<constructor-arg index="1" ref="baseInfo"/>
</bean>1.2 Setter method
Use <property> to set bean properties via generated setter methods.
<bean id="person" class="com.sue.cache.service.test7.Person">
<property name="name" value="susan"/>
<property name="age" value="18"/>
</bean>1.3 Static factory
Reference a static factory method with factory-method and supply arguments through <constructor-arg>.
<bean class="com.sue.cache.service.test7.SusanBeanFactory" factory-method="createPerson">
<constructor-arg index="0" value="susan"/>
<constructor-arg index="1" value="18"/>
</bean>1.4 Instance factory
Define a non‑static factory bean and use factory-bean to create the target bean.
<bean id="susanBeanFactory" class="com.sue.cache.service.test7.SusanBeanFactory"/>
<bean factory-bean="susanBeanFactory" factory-method="createPerson">
<constructor-arg index="0" value="susan"/>
<constructor-arg index="1" value="18"/>
</bean>1.5 FactoryBean
Implement FactoryBean to control object creation; the bean is retrieved with getBean("userFactoryBean") while getBean("&userFactoryBean") returns the factory itself.
<bean id="userFactoryBean" class="com.sue.async.service.UserFactoryBean"/>2. Component annotations
Since Spring 2.5, @Component, @Service, @Repository and @Controller can replace XML definitions. They require component‑scan configuration via <context:component-scan>, @ComponentScan or the implicit scan of @SpringBootApplication.
@Service
public class PersonService {
public String get() { return "data"; }
}3. JavaConfig
Use @Configuration classes with @Bean methods to declare beans programmatically.
@Configuration
public class MyConfiguration {
@Bean
public Person person() { return new Person(); }
}Conditional annotations such as @ConditionalOnClass, @ConditionalOnBean, @ConditionalOnProperty allow fine‑grained activation of beans.
4. Import mechanisms
4.1 @Import of ordinary classes
Import a simple class as a bean with @Import(Role.class).
4.2 @Import of configuration classes
Import another @Configuration class to bring its beans into the context.
4.3 ImportSelector
Implement ImportSelector to return an array of fully‑qualified class names based on custom logic.
4.4 ImportBeanDefinitionRegistrar
Implement ImportBeanDefinitionRegistrar to register beans with custom names, scopes or other metadata.
5. Bean post‑processing
BeanDefinitionRegistryPostProcessorfocuses on bean registration, while BeanFactoryPostProcessor modifies already‑registered bean definitions. Both can be used to add beans programmatically, but using BeanFactoryAware for registration is discouraged.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
