@Repository: indicate persistence layer + @Component + it is supported as a marker for automatic exception translation
@Configuration: indicate the class has @Bean annotated functions + @Component + CGLIB intercepting etc.
@Component vs @Service
They are both same except they indicate the kind of class it is.
@SpringBootApplication
@SpringBootApplication is composed of:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ComponentScan
@ComponentScan annotation performs scanning the packages and creating Beans and gathering into Application Context.
Component-scanning is done starting in the same package as the @SpringBootApplication annotated class is in. So @SpringBootApplication should be at the root level.
@Bean
It is used on methods and not at class level
we annotate methods with @Bean so that Spring can store the method’s result as a Spring bean.
The methods which are annotated with this annotation, have the corresponding class annotated as @Configuration
Name of the bean will be same as the function name
@Component vs @Bean
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class’s source code is editable. @Bean can always be used, but it’s more verbose.
@Component is compatible with Spring’s auto-detection, but @Bean requires manual class instantiation.
Using @Bean, decouples the instantiation of the bean from its class definition. This is why we can use it to make third-party classes into Spring beans. It also means we can introduce logic to decide which of several possible instance options for a bean to use.
This class level annotation indicates that the class will be used by JavaConfig as a source of bean definitions
You should use @Bean methods inside @Configuration annotated class. It enables interception of beans via CGLIB (which can cache and return the bean even if function is directly called)
If @Configuration is not used, @Bean methods run in lite mode without interception with less functionality
Bean Naming
Every bean is identified by its name
Class Level
uses the class name and converts the first letter to lowercase
example: @Component, @Controller, @Service etc.
custom naming: @Component("myBean")
// will create bean with name `loggingService`@Servicepublic class LoggingService {}
Method Level
using @Bean, method name is the name of the Bean.
custom naming: @Bean("myBean")
If the name clashes then, based on the configuration, beans are either overridden or throw exception
Custom naming using @Qualifier
can be used on both class and method level
@Service@Qualifier("logger")public class LoggingService {}
Spring Bean Scopes
Scopes can be defined as well. There are four scopes:
Singleton (default): Per container per Bean
Prototype
Request, Session, Websocket etc. — used only in web aware ApplicationContext
Use default scope and only use other scopes if really necessary
Try to keep singleton spring beans stateless if possible
Singleton Spring bean is not same as singleton design pattern since it is not created once throughout the whole application lifecycle
By default, all Spring beans defined with @Controller, @Service, @Repository, @Component, @Bean or any other bean definition style are eager singletons and spring creates only one instance on application startup.
So we should avoid creating “state” of beans which might co