Saturday, May 14, 2022

Important Spring annotations

 Important Spring annotations


1)@Configuration - 


If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it’s the first call of this method, then the original method’s body will be executed and the resulting object will be registered in the spring context. All further calls just return the bean retrieved from the context.


@Configuration -beans.xml or application.xml


@Component - @beans <bean> @Component annotation marks our classes as beans


2)@SpringBootApplication - @Configuration + @EnableAutoConfiguration + @AutoComponentScan


3)annotations are useless unless we activate them. In order to activate them,

@Configuration - @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource are some of the ones


@Component, @Repository, @Service, @Controller, @RestController, and @Configuration are several ones that <context:component-scan> can detect.


4)@Repository , @Component @Transactional @embadeebale - eqm


5)

@JasonS No. yml files like properties files are used to externalize some application properties. But you can't declare beans in yml.


6)

Spring supports multiple types annotations such as @Component,@Controller,@service @Repository and @Bean. All theses can be found under the org.springframework.stereotype package.



Sr. No. Key @Bean @Component

1

Auto detection

It is used to explicitly declare a single bean, rather than letting Spring do it automatically. 

If any class is annotated with @Component it will be automatically detect by using classpath scan.

2

Spring Container

Bean can be created even class is outside the spring container

We can’t create bean if class is outside spring container

3

Class/Method  Level Annotation

It is a method level annotation

It is a class level annotation

4

@Configuration

It works only when class is also annotated with @Configuration

It works without

@Configuration annotation

5

Use Case

We should use @bean, if you want specific implementation based on dynamic condition.

We can’t write specific implementation based on dynamic condition


7) @Component  = @Configuration +   @Bean