Spring order of #component creation - spring

In what order the #Component classes will be created in spring. is it #configuration annotated class that will be created first ?? Can we specify the order of creation ??

#Component and #Configuration are different types of annotations.
#Component and similar annotations (#Service, #Repository, etc. ) and its JSR-330 counterpart and allow you to declare beans that are to be picked up by autoscanning with <context:component-scan/> or #ComponentScan they register the bean definition for the classes, so they are roughly equivalent to declaring the specified beans with the <bean ... /> tag in XML. This bean types will adhere to the standard proxy creation policies.
#Configuration annotation was designed as the replacement of the XML configuration file. To create #Configuration annotated beans, Spring will always use CGLIB to subclass the #Configuration annotated class, overriding its #Bean annotated method to replace it with the bean lookup method to make singleton beans to be created only once. Despite that, #Configuration annotated classes are still able to use annotated(#Autowired, #Inject etc.) fields and properties to request beans (and even other #Configuration annotated beans too) from the container.
Now answer to your question, you have to annotate the class with #Configuration and then with #ComponentScan(basePackages = { "com.test.*" }) and you can't specify the order of creation.

Related

what is the significance of #Component annotation for Filter in spring boot?

Why do we need to use #Component annotation for Filter in spring boot, why we can't use #Configuration annotation.
Please note: I am not trying to register filter in FilterRegistrationBean.
#Configuration indicates that the class may contain #Bean methods and should be processed to define those beans in the application context. This isn't something that a Filter does so it should not be annotated with #Configuration.
If you look at the source for #Configuration, you'll see that it's annotated with #Component. #Configuration is a special kinds of #Component. In this case, all you need is for the Filter itself to be a bean. It doesn't have any #Bean methods so it's sufficient for it to be annotated with #Component. This will ensure that the Filter is a bean and Spring Boot will the register the Filter bean with the servlet container without any need for a FilterRegistrationBean and without wasting resources unnecessarily processing your Filter's class looking for #Bean methods.

Is the Rest Controller class singleton?

I am learning Spring and Spring Boot.
In my Spring boot app, I am using #RestController annotation for one of my classes which receives requests and processes them accordingly.
#RestController
public class SampleController {
......
}
I want to ask would this class annotated with #RestController be a singleton class?
My thought is that this SampleController would also be a bean and since the default scope is Singleton,
it would be a Singleton class. I want to ask whether I am thinking right.
Yes, you are right. Any bean you creating via annotations #Component, #Service, #Repository, #Controller, #RestController and #Bean of course by default have Singleton scope. Check
Bean scope

Using #PropertySouce in #Component

I tried to use #PropertySource in a #Component like:
#Component
#PropertySource("somepropertiesfile.properties")
public class Student {
...
}
It worked fine.
I want to understand, what is the different between using #PropertySource with #Component and #PropertySource with #Configuration.
Is there any difference or impact of using #PropertySource with #Component.
Configuration is itself a Component type, look into the #Configuration annotation implementation below.
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Configuration {
}
From API
Component: Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Configuration: Indicates that a class declares one or more #Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
The #Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container. These are same as Spring’s XML configuration. You can use #Bean annotated methods with any Spring #Component, however, they are most often used with #Configuration beans.
Here also you can use #PropertySource in #Component class but these are most suitable for #Configuration classes as it is a configuration related task.
You can refer Doc for detailed information.

How SpringBoot dependency injection works with different type of annotations

I recently started exploring Spring Boot. I see that there are 2 ways to define Beans in Spring Boot.
Define #Bean in the class annotated with #SprinBootApplication
Define #Bean in a class annotated with #Configuration
I am also confused about stereo-type annotation #Repository #Service #Controller etc.
Can someone please explain how dependency-injection works with these annotations?
Yes it is possible.
Either you use #Bean in any of your #Configuration or #SpringBootApplication class or mark the bean classes explicitly with annotations like #Service, #Component #Repository etc.
#Service or #Component
When you mark a class with #Service or #Compoenent and if spring's annotation scanning scope allows it to reach to the package, spring will register the instances of those classes as spring beans.
You can provide the packages to be included/excluded during scan with #ComponentScan
#Bean
#Beans are marked on factory methods which can create an instance of a particular class.
#Bean
public Account getAccount(){
return new DailyAccount();
}
Now in you application you can simply #Autowire Account and spring will internally call its factory method getAccount, which in turn returns an instance of DailyAccount.
There is a simple difference of using #Bean vs #Service or #Compoenent.
The first one makes your beans loosely coupled to each other.
In the #Bean, you have flexibility to change the account implementation without even changing any of the account classes.
Consider if your classes instantiation is a multi-step operation like read properties values etc then you can easily do it in your #Bean method.
#Bean also helps if you don't have source code access to the class you are trying to instantiate.
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
You need to opt-in to auto-configuration by adding the #EnableAutoConfiguration or #SpringBootApplication annotations to one of your #Configuration classes.
You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies. For simplicity, we often find that using #ComponentScan (to find your beans) and using #Autowired (to do constructor injection) works well.
One way is to define #Bean in the class annotated with
#SprinBootApplication
If you see #SprinBootApplication it is combination of many annotation, and one of them is #Configuration. So when you define #Bean in the Main class, it means it's inside #Configuration class.
According to Configuration docs :
Indicates that a class declares one or more #Bean methods and may be
processed by the Spring container to generate bean definitions and
service requests for those beans at runtime.
class annotated with #Configuration
When you define #Bean is a class annotated with #Configuration class, it means it is the part of spring configuration all the Beans define in it all available for Dependency-Injection.
I have also seen some code where neither of the 2 above approaches
have been used and yet dependency injection works fine. I have tried
to research a lot on this but could not find any concrete answer to
this. Is this possible?
I am assuming you are talking about Sterio-type annotation. Every sterio type annotation has #Component, according to docs :
Indicates that an annotated class is a "component". Such classes are
considered as candidates for auto-detection when using
annotation-based configuration and classpath scanning.

When annotating a class with #Component, does this mean it is a Spring Bean and Singleton?

Being fairly new to Spring I have a question about annotating a class. When annotating a class with #Component does this mean this class will be a Spring Bean and by default a singleton?
Yes, that is correct, #Component is a Spring bean and a Singleton.
If the class belongs to the service layer you may want to annotate it with #Service instead
But have in mind that in order for these annotations to be detected, you need to place this line in applicationContext.xml:
<context:component-scan base-package="com.yourcompany" />
About singletons - spring beans are all in singleton scope by default. The only thing you have to have in mind is that you should not store state in field variables (they should only hold dependencies). Thus your application will be thread-safe, and you won't require a new instance of a bean each time. In other words, your beans are stateless.
By default - Yes.
However, you can override this behavior using the #Scope annotation. For example: #Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Resources