M01 Q10 Dependency injection. Component scanning, Stereotypes and Meta-Annotations. Scopes for Spring beans.
- Dependency injection using Java configuration?
- Dependency injection using annotations (@Component, @Autowired)?
- Component scanning, Stereotypes and Meta-Annotations?
- Scopes for Spring beans?
- What is the default scope?
Dependency Injection using Java Configuration
When using Dependency Injection using Java Configuration you need to explicitly define all your beans and you need to use @Autowired on @Bean method level to inject dependencies.
There are different ways to define application context and also different ways to define how beans should be discovered. One way is to create Java configuration and list all of the beans that will be used.
To do this you need to annotate configuration class with @Configuration. Then you create methods. And each method contains one of the beans and it should be annotated with @Bean.
In my project, there are three beans and SpringBean1 has two dependencies SpringBean2 and SpringBean3.
And if you need to have some dependencies to bean you need to use @Autowired.
The order of the creation is on purpose because Spring knows that to create SpringBean1 it needs to create SpringBean2 and SpringBean3 firstly.
Dependency injection using annotations (@Component, @Autowired)
Firstly, you need to create bean classes with annotation @Component
If you need to supply some dependencies you are using @Autowired annotation.
Also, you need to create the configuration. But this time this configuration will not contain any of the classes or beans. Instead, we will use the @ComponentScan annotation. It says to Spring to search for the beans on the classpath.
Component scanning
Process in which Spring is scanning Classpath in search for classes annotated with stereotypes annotations (@Component, @Repository, @Service, @Controller, …) and based on those creates beans definitions.
Simple component scanning within Configuration package and all sub packages.
Simple component scanning means to scan all of the beans within this package.
Advanced Component Scanning Rules
Also, @ComponentScan can have different values provided to it. So we can control how Spring will scan classpath.
You can add include and exclude filters to @ComponentScan
Also, keep in mind that you can define the packages that will scan through the string.
But also, it is possible to provide a class. And based on this class Spring will read the package and will scan all classes in the package where this class is located.
Stereotypes
Stereotypes are annotations applied to classes to describe role which will be performed by this class. Spring discovered classes annotated by stereotypes and creates bean definitions based on those types.
- @Component – generic component in the system, root stereotype, candidate for autoscanning
- @Service – class will contain business logic
- @Repository – class is a data repository (used for data access objects, persistence)
- @Controller – class is a controller, usually a web controller (used with
@RequestMapping)
You can change all the above annotations to @Component and it will work. Because these annotations define the role of the class within the system. Usually, they don't change the way how the class works. It is more like information for the developer who read the code.
Meta-Annotations
Meta-annotations are annotations that can be used to create new annotations.
For example, @RestController annotation is using @Controller and @ResponseBody to define its behaviour.
@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController
Scopes of Spring Beans
Spring, by default, provides you with six scopes. Singleton and Prototype are used for the standalone applications
Request scope means that every time when you make HTTP request you will have a new Spring Bean.
The application scope is one instance per deployment in the container like Tomcat. And it ties to the lifecycle of the ServletContext.
When bean has not any additional annotation it will have a default scope - Singleton.
Next bean has Prototype scope. That means every time when we retrieve this bean we will get a new instance of it.
Now let's look at web scopes in the code. To do this we need to create a web application and define new beans.
SpringBean1 will have a Singleton scope.
SpringBean2 will have a Prototype scope.
SpringBean3 will have a Request scope.
SpringBean4 will have a Session scope.
SpringBean5 will have an Application scope.