Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Component Spring annotation standard scope

Tags:

java

spring

What's the default scope of Spring's @Component annotation?

If you don't define a scope at all, is it application scoped? The Spring documentation does not say anything about default scope.

like image 682
Master Azazel Avatar asked Mar 04 '17 18:03

Master Azazel


People also ask

What is the scope of @component in Spring?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

What is the default scope of Spring?

Spring's default scope is singleton.

Are @component singletons?

Yes, that is correct, @Component is a Spring bean and a Singleton. 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).

What is the difference between @configuration and @component in Spring?

The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.


1 Answers

The singleton scope is the default scope in Spring.

This is said here

And also in the current doc you find

singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container.

like image 100
thopaw Avatar answered Sep 18 '22 16:09

thopaw