0% found this document useful (0 votes)
6 views19 pages

spring4

The document provides an overview of Spring Boot annotations, which serve as metadata to enhance program functionality without altering the code's operation. It details core annotations like @Autowired, @Configuration, and @Component, as well as Spring Boot's dependency management features that simplify version control and configuration. Additionally, it discusses application properties management through application.properties and application.yml files, along with the use of Spring Boot starters for dependency management.

Uploaded by

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

spring4

The document provides an overview of Spring Boot annotations, which serve as metadata to enhance program functionality without altering the code's operation. It details core annotations like @Autowired, @Configuration, and @Component, as well as Spring Boot's dependency management features that simplify version control and configuration. Additionally, it discusses application properties management through application.properties and application.yml files, along with the use of Spring Boot starters for dependency management.

Uploaded by

Candy Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Spring Boot Annotations

Spring Boot Annotations is a form of metadata that provides data about a program. In
other words, annotations are used to provide supplemental information about a
program. It is not a part of the application that we develop. It does not have a direct
effect on the operation of the code they annotate. It does not change the action of
the compiled program.

Core Spring Framework Annotations


@Required: It applies to the bean setter method. It indicates that the
annotated bean must be populated at configuration time with the required
property, else it throws an exception BeanInitilizationException.
@Autowired: Spring provides annotation-based auto-wiring by providing
@Autowired annotation. It is used to autowire spring bean on setter methods,
instance variable, and constructor. When we use @Autowired annotation, the
spring container auto-wires the bean by matching data-type.

@Configuration: It is a class-level annotation. The class annotated with


@Configuration used by Spring Containers as a source of bean definitions.

@ComponentScan: It is used when we want to scan a package for beans. It


is used with the annotation @Configuration. We can also specify the base
packages to scan for Spring Components.

@Bean: It is a method-level annotation. It is an alternative of XML <bean> tag. It


tells the method to produce a bean to be managed by Spring Container.
Spring Framework Stereotype Annotations
@Component: It is a class-level annotation. It is used to mark a Java class as a
bean. A Java class annotated with @Component is found during the classpath.
The Spring Framework pick it up and configure it in the application context as
a Spring Bean.

@Controller: The @Controller is a class-level annotation. It is a


specialization of @Component. It marks a class as a web request handler.
It is often used to serve web pages. By default, it returns a string that
indicates which route to redirect. It is mostly used
with @RequestMapping annotation.

@Service: It is also used at class level. It tells the Spring that class contains
the business logic.

@Repository: It is a class-level annotation. The repository is a DAOs (Data


Access Object) that access the database directly. The repository does all the
operations related to the database.
Spring Boot Annotations
@EnableAutoConfiguration: It auto-configures the bean that is present in the
classpath and configures it to run the methods. The use of this annotation is reduced in
Spring Boot 1.2.0 release because developers provided an alternative of the
annotation, i.e. @SpringBootApplication.

@SpringBootApplication: It is a combination of three


annotations @EnableAutoConfiguration, @ComponentScan, and @Configuration.
Spring Boot Dependency Management
Spring Boot manages dependencies and configuration automatically. Each
release of Spring Boot provides a list of dependencies that it supports. The list of
dependencies is available as a part of the Bills of Materials (spring-boot-
dependencies) that can be used with Maven. So, we need not to specify the
version of the dependencies in our configuration. Spring Boot manages itself.
Spring Boot upgrades all dependencies automatically in a consistent way when
we update the Spring Boot version.

Advantages of Dependency Management

It provides the centralization of dependency information by specifying the Spring


Boot version in one place. It helps when we switch from one version to another.

It avoids mismatch of different versions of Spring Boot libraries.

We only need to write a library name with specifying the version. It is helpful in
multi-module projects.
Maven Dependency Management System
The Maven project inherits the following features from spring-boot-starter-
parent:

The default Java compiler version

UTF-8 source encoding

It inherits a Dependency Section from the spring-boot-dependency-pom. It


manages the version of common dependencies. It ignores the <version> tag for
that dependencies.

Dependencies, inherited from the spring-boot-dependencies POM

Sensible resource filtering

Sensible plugin configuration


Inheriting Starter Parent
The following spring-boot-starter-parent inherits automatically when we
configure the project.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version>
<!-- lookup parent from repository -->
<relativePath/>
</parent>

Changing the Java version


We can also change the Java version by using
the <java.version> tag.
<properties>
<java.version>1.8</java.version>
</properties>
Adding Spring Boot Maven Plugin
We can also add Maven plugin in our pom.xml file. It wraps the project into
an executable jar file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Spring Boot without Parent POM
If we don't want to use spring-boot starter-parent dependency, but still want to
take the advantage of the dependency management, we can use <scope> tag,
as follows:
<dependencyManagement>
<dependencies>
<dependency><!--
Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

The above dependency does not allow overriding. To achieve the overriding,
we need to add an entry inside the <dependencyManagement> tag of our
project before the spring-boot-dependencies entry.
For example, to upgrade another spring-data-releasetrain, add the following
dependency in the pom.xml file.

<dependencyManagement>
<dependencies>
<!--Override Spring Data release train-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring Boot Application Properties
Spring Boot Framework comes with a built-in mechanism for application
configuration using a file called application.properties. It is located inside
the src/main/resources folder, as shown in the following figure.
Spring Boot provides various properties that can be configured in
the application.properties file. The properties have default values. We can set a
property(s) for the Spring Boot application. Spring Boot also allows us to define our
own property if required.

The application.properties file allows us to run an application in a different


environment. In short, we can use the application.properties file to:

Configure the Spring Boot framework

define our application custom configuration properties


Example of application.properties

#configuring application name


spring.application.name = demoApplication
#configuring port
server.port = 8081

In the above example, we have configured the application name and port.
The port 8081 denotes that the application runs on port 8081.

Note: The lines started with # are comments.


YAML Properties File
Spring Boot provides another file to configure the properties is called yml file.
The Yaml file works because the Snake YAML jar is present in the classpath.
Instead of using the application.properties file, we can also use the
application.yml file, but the Yml file should be present in the classpath.
Example of application.yml
spring:
application:
name: demoApplication
server:
port: 8081
In the above example, we have configured the application name and port. The
port 8081 denotes that the application runs on port 8081.
Spring Boot Property Categories
There are sixteen categories of Spring Boot Property are as follows:
Core Properties
Cache Properties
Mail Properties
JSON Properties
Data Properties
Transaction Properties
Data Migration Properties
Integration Properties
Web Properties
Templating Properties
Server Properties
Security Properties
RSocket Properties
Actuator Properties
DevTools Properties
Testing Properties
Spring Boot Starters
Spring Boot provides a number of starters that allow us to add jars in the classpath.
Spring Boot built-in starters make development easier and rapid. Spring Boot
Starters are the dependency descriptors.

In the Spring Boot Framework, all the starters follow a similar naming pattern: spring-
boot-starter-*, where * denotes a particular type of application. For example, if we
want to use Spring and JPA for database access, we need to include the spring-boot-
starter-data-jpa dependency in our pom.xml file of the project.

Third-Party Starters
We can also include third party starters in our project. But we do not use spring-
boot-starter for including third party dependency. The spring-boot-starter is reserved
for official Spring Boot artifacts. The third-party starter starts with the name of the
project. For example, the third-party project name is abc, then the dependency name
will be abc-spring-boot-starter.

You might also like