0% found this document useful (0 votes)
119 views4 pages

Spring Boot Annotations Overview

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views4 pages

Spring Boot Annotations Overview

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Spring Boot Annotations

1. @SpringBootApplication
 Firstly it use to mark java class as starting point of application
 And also this annotation is equivalent to three annotation which is
@EnableAutoConfiguration + @ComponentScan + @Configuration
 Now in @EnableAutoConfiguration will Enable the Auto configuration means it will Automatically Configure
if any Spring configuration based on the Jar dependency If any present in classpath so it will auto configured
in our project.(We can use attribute “exclude” for exclude the specific auto configuration)
 For Example if we found spring-boot-starter-web Dependency in [Link] then Spring boot application will
automatically configure the web application context and JPA persistence context.
 @ComponentScan this is just a simple annotation where it will scan our All the Components is present in
Specified package for example it will scan like @Component, @Service, @Repository and @Controller and
then it will register as a Bean in Spring Application Context.
 If we need to scan out of the package so in that case we need to add
@ComponentScan(basePackage = {“otherPackageComponent”}) ) in our Configuration class.
 @Configuration will help us to identifying Beans or class which will returning Beans for Spring Dependency
Injection which will manage by the IOC Container. In simple word this Annotation marks a class as source of
Bean Definition.

2. @Bean
 It will tell us ````````````````````````````````````````````````````````````````````````````````````````````````````````````+this will returning
the Bean to be managed by Spring Container.
 It will create a managed instance of particular return type of method.

3. @Component
 This annotation will tell us to this class is the Component which will manage by spring and we will use it with
autowiring.

4. @Autowired
 This annotation is provide a feature which we can achieve Dependency Injection Automatically.
 It will inject automatically the Object from Spring Container by matching type of this particular Object.
 It help us to autowire the bean without creating an object using new keyword.
 We can applied to Constructor, Field, or Setter Method.

@Component
 @Controller
 @Service
 @Repository

5. @Controller
 This Annotation will use for define the class as component which will used a Restful web service to handle
Requests and Responses.
 @Controller – Spring framework will always looking for HTML or view page for the returning string from method.
 If we annoted @Controller it will always expect as return type Model and View
 @RestController – It will return any thing we have in our method.

6. @Service
 This Annotation will use for define the class as component which will used for Service Layer.
 In Simple meaning this annotation is use for having the Business Logic of the application it this Specific Class.
7. @Repository
 This Annotation will use for define the class as component which will used for Represent the DAO layer of
the application to deals with the database CRUD operations.

8. @Qualifier
 This annotation will Qualify the Specific Bean if there is more than One Bean is available.
 For example @Autowired annotation will check the type of Object and if there is two or more Same type of
object is present then we will use @Qualifier annotation with Autowiring to avoid ambiguity problem.

9. @Lazy
 In spring boot a while defining spring bean then it will create Spring Beans in Container and now if we want
this Spring bean is created as per requirement while we inject this bean then it will have to create then we
will use @Lazy annotation.

Stereotype Annotation @Component, @Service, @RestController, @Controller, @Repository

10. @Value
 It will use for to inject the value explicitly from [Link] file with define key as attribute
 For example @Value(${[Link]}) – it will inject the port no. of application in define variable.

11. @PropertySource
 This annotation is use in Class level for defining the source of the properties which we will use by @Value
attribute.

12. @ConfigurationProperties
 It will use for configure the specific properties form properties file.
 For Example:
In [Link] file

[Link] = abcd
[Link] = 22

In [Link] file

@ConfigurationProperties(prefix = “devname”)
public class somClass{
private String name;
private String age;
}
 In this Example devname is the property in properties file and we will use or inject directly in Variable.

13. @Profile
 This will use for defining the which environment profile use for application.
 [Link] = dev

14. @Scope
 If we define the scope of the bean we will use this annotation.
 For example we use this @Scope(“singletone”) or @Scope(“prototype”)
Scope of Bean :-
@Scope – It will tell scope of the bean means if we want to declare the scope of the Bean.
In that commonly we use singleton or prototype.

Type of Scope
Singleton – by default scope of spring bean – it will return same object reference every time in entire application.
Prototype – it will return always new object reference – it will create new object every time
Request – for HHTP request objects – it will create new instance for each HTTP request.
Session – for only session objects – it will create single instance of the bean per web socket session

15. @RestController
This is specialized version of Controller that include @ResponseBody means it directly return data rather than
view.
It will return data in XML and Json format.

16. @RequestMapping

17. @GetMapping

18. @PostMapping

19. @PutMapping

20. @DeleteMapping

21. @RequestBody
It will bind the data from request into Java Object.

22. @PathVariable

23. @RequestParam

24. @ControllerAdvice
Provide global exception handling.

25. @ExceptionHandler

26. @Entity
 It will tell to JPA or Hibernate this class will perform to Database Related Operations.
 Use to mark any class as Entity.

27. @Table
 It will define to JAP or Hibernate to create the Table as the name provide and if we did not use this
annotation the @Entity annotation will do automatically as per class name.

28. @Column
It specify the column name from class field and modify the column name.

29. @GeneratedValue
It will auto generate value for this column.

30. @GeneratorType

31. @Transactional

32. @Temporal
This will format the date.
33. @Transient
It will tell to hibernate not to save this class field.

34. @Data

35. @CrossOrigin

36. @Secured

37. @PreAuthorize

38. @PermitAll

39. Caching Annotation

40. @EnableCaching

41. @Cacheable

42. @CachePut

43. @CacheEvict

AOP(Aspect Oriented Annotation)

44. @Aspect
45. @Pointcut
46. @AfterRunning
47. @AfterThowing
48. @Around
49. @Before

Common questions

Powered by AI

The @Autowired annotation in Spring enables automatic dependency injection by matching the type of the annotated object with a bean available in the Spring Container, bypassing the need to create an object using the 'new' keyword. When there are multiple beans of the same type, the @Qualifier annotation helps resolve ambiguity by specifying which bean should be injected. By using @Qualifier alongside @Autowired, developers can explicitly define the desired bean to avoid conflicts that arise due to multiple candidates being present for autowiring .

The @SpringBootApplication annotation is significant because it marks a Java class as the starting point of a Spring Boot application. It integrates the functionalities of three separate annotations: @EnableAutoConfiguration, @ComponentScan, and @Configuration. The @EnableAutoConfiguration part automatically configures the application based on the jar dependencies available in the classpath, such as configuring web application context if spring-boot-starter-web is present. The @ComponentScan annotation scans for components like @Component, @Service, @Repository, and @Controller within a specified package, and registers them as beans in the Spring Application Context. Lastly, the @Configuration component of this annotation identifies classes as sources of bean definitions, facilitating Spring's dependency injection .

In JPA or Hibernate, the @Entity annotation marks a class as an entity and maps it to a database table. The @Table annotation provides additional options for customizing the table configuration, such as specifying the table name or schema. If the @Table annotation is omitted, the entity will default to mapping the class to a table with the same name as the class itself. This default behavior effectively simplifies the entity definition in cases where no custom table configuration is required .

The @EnableAutoConfiguration annotation in a Spring Boot application simplifies the initial project setup by automatically configuring commonly used Spring components based on the dependencies present in the classpath. When included, it scans for beans, including web components, databases, or JPA, required by a project, sets them up, and registers them without requiring explicit configuration. This auto-configuration mechanism reduces boilerplate code, accelerating development by leveraging a convention-over-configuration approach, making it easier for developers to build applications quickly without delving into comprehensive manual configuration .

The @Value annotation in a Spring application is used to inject property values from the application.properties file into Java variables. By referencing a key inside the annotation, such as @Value(${server.port}), it assigns the corresponding value, like the server's port number, to a variable within the application. The @PropertySource annotation complements @Value by defining the locations of the properties files at the class level, which are then used by @Value to inject values. This setup ensures that configuration values are centralized and can be easily modified without altering source code .

The @Transactional annotation provides significant advantages in managing transactions within a Spring application by ensuring that a series of operations complete successfully or roll back in case of failure, thus maintaining data integrity. By applying this annotation to service methods, Spring transparently handles the transaction management, reducing the complexity associated with manually managing transactions. This declarative approach simplifies the code, ensures consistency across database operations, and automates rollback in case of runtime exceptions, minimizing the risk of data corruption. It is crucial for applications requiring reliable data processing .

Applying the @Lazy annotation to Spring beans defers the initialization of the bean until it is actually needed rather than at startup. This can be advantageous in improving startup times for applications with numerous beans, as it avoids the overhead of creating beans that aren't immediately necessary. It also conserves memory and CPU resources in scenarios where certain beans might not be used during a particular execution of the application. The @Lazy annotation is particularly useful in large-scale applications to optimize resource usage and improve performance by loading beans only when required .

The @CrossOrigin annotation helps manage Cross-Origin Resource Sharing (CORS) concerns by allowing web applications to specify which origins are permitted to access resources. This enhances both security and usability by granting controlled access to resources, which is critical when a web application needs to be accessed from different domains in a secure manner. By specifying allowed origins, methods, and headers, @CrossOrigin prevents unauthorized access while facilitating legitimate cross-domain requests, thus enabling seamless interactions in varied deployment environments .

The @ComponentScan annotation in Spring is used to specify the packages that need to be scanned for components, such as @Component, @Service, @Repository, and @Controller. This mechanism allows Spring to automatically detect and register beans within the specified packages into the Spring Application Context. If components need to be scanned outside the package where the main application class is located, a custom base package can be defined using @ComponentScan(basePackages = {"otherPackage"}). Through this annotation, Spring supports the organization of bean definitions across different packages and manages them effectively .

The @Controller annotation in a Spring MVC application is used to define a class as a component that handles web requests and typically returns a ModelAndView object, pointing to a view like an HTML page. In contrast, the @RestController annotation is a specialized version of @Controller that includes the @ResponseBody behavior by default, meaning it directly returns data such as JSON or XML without needing a view resolver. @RestController is typically used when building RESTful web services .

You might also like