Spring Framework: Bean definition and creation
Fields in org.springframework.beans.factory.config.BeanDefinition Fields Meaning name Name of bean parentName beanClassName String of bean class scope singleton/prototype/request/session/refresh... lazyInit true if actual bean will be instantiated when being called dependsOn beans which is essential for this bean autowireCandidate to specify if this can be injected into other bean in autowire operation primary true is the first candidate in autowire operation factoryBeanName with factoryMethodName to specify class method to create a bean factoryMethodName initMethodName initialization method after bean is instantiated destroyMethodName last method called before bean destroyed role ROLE_APPLICATION/ROLE_SUPPORT/ROLE_INFRASTRUCTURE which described in org.springframework.beans.factory.config.BeanDefinition org.springframework.context.annotation.ComponentScan annotation The base package of the class annotated with @ComponentScan will become the base package start to scanning. And the classes will following annotations will be picked up. @Component All annotated with @Component @Configuration @Repository @Service @Controller Please note that org.springframework.boot.autoconfigure.SpringBootApplication is annotated with @ComponentScan annotation. Function of org.springframework.context.annotation.Configuration annotation This annotation is for annotates class (configuration class) which provide methods of creating bean (will annotated with @bean) Configuration classes behave like normal beans, and will be created at the beginning of bean creation of starting ApplicationContext. proxyBeanMethods element: default is true, it is to avoid double bean instance creation when a method in the configuration class refers to another method of the class. ConditionalOnXXX annotation Following are commonly used annotations. Annotation Function ConditionalOnBean Defined bean can be found in BeanFactory ConditionalOnClass Defined class can be found in Classpath ConditionalOnMissingBean Defined bean cannot be found in BeanFactory ConditionalOnMissingClass Defined class cannot be found in Classpath ConditionalOnProperty To check particular value of a property ConditionalOnResource Defined resource can be found ConditionalOnSingleCandidate Only one bean can be found in BeanFactory Control code of ConditionalOnXXX annotation Below is the definition of ConditionalOnClass annotation package org.springframework.boot.autoconfigure.condition; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Conditional; @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnClassCondition.class) public @interface ConditionalOnClass { Class[] value() default {}; String[] name() default {}; } It is annotated with Conditional annotation, the class OnClassCondition is the actual implementation to define the behavior of ConditionalOnClass annotation. Multiple annotation If more than one ConditionalOnXXX annotation are annotated on a configuration class. This will be instantiated only if all annotations hold (i.e. AND condition). On following example @Configuration(proxyBeanMethods = false) @ConditionalOnMissingBean(ReactiveWebServerFactory.class) @ConditionalOnClass({ org.apache.catalina.startup.Tomcat.class }) static class EmbeddedTomcat { @Bean TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory( ObjectProvider connectorCustomizers, ObjectProvider contextCustomizers, ObjectProvider
Fields in org.springframework.beans.factory.config.BeanDefinition
Fields | Meaning |
---|---|
name | Name of bean |
parentName | |
beanClassName | String of bean class |
scope | singleton/prototype/request/session/refresh... |
lazyInit | true if actual bean will be instantiated when being called |
dependsOn | beans which is essential for this bean |
autowireCandidate | to specify if this can be injected into other bean in autowire operation |
primary | true is the first candidate in autowire operation |
factoryBeanName | with factoryMethodName to specify class method to create a bean |
factoryMethodName | |
initMethodName | initialization method after bean is instantiated |
destroyMethodName | last method called before bean destroyed |
role | ROLE_APPLICATION/ROLE_SUPPORT/ROLE_INFRASTRUCTURE which described in org.springframework.beans.factory.config.BeanDefinition |
org.springframework.context.annotation.ComponentScan annotation
The base package of the class annotated with @ComponentScan will become the base package start to scanning. And the classes will following annotations will be picked up.
- @Component
All annotated with @Component
- @Configuration
- @Repository
- @Service
- @Controller
Please note that org.springframework.boot.autoconfigure.SpringBootApplication is annotated with @ComponentScan annotation.
Function of org.springframework.context.annotation.Configuration annotation
This annotation is for annotates class (configuration class) which provide methods of creating bean (will annotated with @bean)
Configuration classes behave like normal beans, and will be created at the beginning of bean creation of starting ApplicationContext.
proxyBeanMethods element: default is true, it is to avoid double bean instance creation when a method in the configuration class refers to another method of the class.
ConditionalOnXXX annotation
Following are commonly used annotations.
Annotation | Function |
---|---|
ConditionalOnBean | Defined bean can be found in BeanFactory |
ConditionalOnClass | Defined class can be found in Classpath |
ConditionalOnMissingBean | Defined bean cannot be found in BeanFactory |
ConditionalOnMissingClass | Defined class cannot be found in Classpath |
ConditionalOnProperty | To check particular value of a property |
ConditionalOnResource | Defined resource can be found |
ConditionalOnSingleCandidate | Only one bean can be found in BeanFactory |
Control code of ConditionalOnXXX annotation
Below is the definition of ConditionalOnClass annotation
package org.springframework.boot.autoconfigure.condition;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
Class>[] value() default {};
String[] name() default {};
}
It is annotated with Conditional annotation, the class OnClassCondition is the actual implementation to define the behavior of ConditionalOnClass annotation.
Multiple annotation
If more than one ConditionalOnXXX annotation are annotated on a configuration class. This will be instantiated only if all annotations hold (i.e. AND condition). On following example
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(ReactiveWebServerFactory.class)
@ConditionalOnClass({ org.apache.catalina.startup.Tomcat.class })
static class EmbeddedTomcat {
@Bean
TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers,
ObjectProvider<TomcatProtocolHandlerCustomizer>> protocolHandlerCustomizers) {
//...
}
}
This will be instantiated if org.apache.catalina.startup.Tomcat class is found and bean which provides org.springframework.boot.web.reactive.server.ReactiveWebServerFactory is not found in BeanFactory.
ConditionalOnProperty annotation
There is little special of ConditionalOnProperty annotation because . On following example
@AutoConfiguration
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
//...
}
If property spring.aop.auto is not found, the condition holds. If the value of property spring.aop.auto is true, the condition holds also. Conditions don't hold if the value of property spring.aop.auto is other than true.
Auto-configuration class provided by Spring Boot
In the spring-boot-autoconfigure package, many auto-configuration classes exist in order to create essential beans with default properties when there are no such beans defined in the application.