Annotation in Spring Boot

 Annotation in Spring Boot


To achieve dependency injection and different design goals we use annotations.
Annotation in Spring Boot



Following is the list of most commonly used annotations in Spring

πŸ‘‰1. @Component
use to make dependent class belong to Spring container. By default, it will create the object of the class in the Spring container using a singleton design pattern (only one object will be created).

Ex.
@Component
class Alpha extends Alien{......}











πŸ‘‰2. @Autowire
use to link objects available in Spring Container.

Ex.
@Autowire
Alien jaadoo;











πŸ‘‰3.  @Scope(value="prototype")
When we don't want to use the default singleton design pattern and want to create multiple objects when we ask for it only then.

Ex.
@Component
@Scope(value="prototype")
class Alpha extends Alien{......}

πŸ‘‰4. @Qualifier("name")

Use to give a specific name to the object in the spring container instead of the default name.

Ex.
@Component("jadoo")
class Alpha extends Alien{......}

class T{
@Autowire
@Qualifier("jadoo")
Alien jaadoo;
}
In the above example, the default name is alpha but we changed it to jadoo by using annotation.

πŸ‘‰5. @ConditionalOnProperty( propertyName = "do something")
Use to set(enable/disable) some conditions based on property.
For example, we want to enable the scheduling of some tasks based on the time we can use this annotation.
@ConditionalOnProperty(name = ""spring.enable.scheduling" )

πŸ‘‰6. @Scheduled(cron = "${schedularName}")
Use to schedule tasks based on time and time will be defined in the application.properties based on schedularName.

πŸ‘‰7. @Entity("EntityName")
By default entity name is class name Entity represents POJO which is persistence to the database. One entity iteration is equal to 1 row in the table. 

πŸ‘‰8. @Table(name = "TABLE_NAME")
Represents table name in database.

πŸ‘‰9. @Id
Represents the primary key of the database.

πŸ‘‰10.@Column
Represent column Name in Table.

πŸ‘‰11.@Repository
It represents the class is a repository we extend Repo also to use default methods like save, saveAll etc.


No comments:

For Query and doubts!

Powered by Blogger.