0% found this document useful (0 votes)
360 views

Spring. Springboot.: What Are The Spring Boot Annotations?

Spring vs Spring Boot: - Spring is a framework for building web applications while Spring Boot makes it easier to create stand-alone Spring applications. - Spring Boot favors convention over configuration and has built-in dependencies so applications can be started using "java -jar". Some key Spring Boot annotations: - @SpringBootApplication combines configuration, component scanning and auto-configuration annotations. - @RestController marks classes that handle web requests and returns JSON, XML or other types. - @RequestMapping maps HTTP requests to handler methods and provides routing information. Spring Boot enables auto-configuration based on dependencies and conditions. Common conditions include checking for classes, beans, properties, or web application

Uploaded by

canek_celula_jrg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
360 views

Spring. Springboot.: What Are The Spring Boot Annotations?

Spring vs Spring Boot: - Spring is a framework for building web applications while Spring Boot makes it easier to create stand-alone Spring applications. - Spring Boot favors convention over configuration and has built-in dependencies so applications can be started using "java -jar". Some key Spring Boot annotations: - @SpringBootApplication combines configuration, component scanning and auto-configuration annotations. - @RestController marks classes that handle web requests and returns JSON, XML or other types. - @RequestMapping maps HTTP requests to handler methods and provides routing information. Spring Boot enables auto-configuration based on dependencies and conditions. Common conditions include checking for classes, beans, properties, or web application

Uploaded by

canek_celula_jrg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

What’s the difference between Spring vs SpringBoot

 Spring.
 SpringBoot.
What is Spring Boot?
What are the advantages of Spring Boot?
What are the features of Spring Boot?
How to create Spring Boot application using Spring Starter Project Wizard?
Spring Vs Spring Boot?
How to create Spring Boot application using Maven?
How to create Spring Boot project using Spring Initializer?
How to create Spring Boot project using boot CLI?
How to create simple Spring Boot application?
What is a Dispatcher Servlet
Who is configuring Dispatcher
What does Dispatcher do
How Bean obj converted to JSON
Who is configuring error mapping

What are the Spring Boot Annotations?


@SpringBootApplication
@EnableAutoConfiguration
Auto-Configuration Conditions
@ConditionalOnClass and @ConditionalOnMissingClass
@ConditionalOnBean and @ConditionalOnMissingBean
@ConditionalOnProperty
@ConditionalOnResource
@ConditionalOnWebApplication and @ConditionalOnNotWebApplication
@ConditionalOnWebApplication
@ConditionalExpression
@Conditional
@RestController
@RequestMapping
@RequestBody
@PathVariable
@RequestParam
Process to Build a RESTful Web Services
I. Add the Spring Boot Starter to Maven:
What is Spring Boot Actuator
What is thymeleaf?
How to use thymeleaf?
How to connect Spring Boot to the database using JPA
How to connect Spring Boot application to database using JDBC
What is Spring Boot dependency management
What are the Spring Boot properties?
What are the Spring Boot Starters?

Cómo crear y desplegar microservicios con Spring


Boot,

https://www.adictosaltrabajo.com/2020/12/22/co
mo-crear-y-desplegar-microservicios-con-spring-
boot-spring-cloud-netflix-y-docker/
What’s the difference between Spring vs SpringBoot. @

 Spring.
- XML based configuration.
- Application creation/execution takes more time.
- More boiler plate code.

 SpringBoot.
- Annotation-based.
- Application creation/execution is quicker.
- Reduced amount of boiler plate code.

What is Spring Boot? @

Spring Boot is a Spring module which provides RAD (Rapid Application Development)
feature to Spring framework.

It is used to create stand alone spring based application that you can just run because it
needs very little spring configuration.

What are the advantages of Spring Boot? @

o Create stand-alone Spring applications that can be started using java -jar.
o Embed Tomcat, Jetty or Undertow directly. You don't need to deploy WAR files.
o It provides opinionated 'starter' POMs to simplify your Maven configuration.
o It automatically configure Spring whenever possible.

What are the features of Spring Boot? @

o Web Development
o Spring Application
o Application events and listeners
o Admin features

How to create Spring Boot application using Spring Starter Project Wizard? @

There is one more way to create Spring Boot project in STS (Spring Tool Suite). Creating
project by using IDE is always a convenient way. Follow the following steps in order to
create a Spring Boot Application by using this wizard.

Spring Vs Spring Boot? @


Spring is a web application framework based on Java. It provides tools and libraries to
create a complete cutomized web application.

Wheras Spring Boot is a spring module which is used to create spring application project
that can just run.

How to create Spring Boot application using Maven? @

There are multiple approaches to create Spring Boot project.

o Spring Maven Project


o Spring Starter Project Wizard
o Spring Initializer
o Spring Boot CLI

How to create Spring Boot project using Spring Initializer? @

It is a web tool which is provided by Spring on official site. You can create Spring Boot
project by providing project details.

How to create Spring Boot project using boot CLI? @

It is a tool which you can download from the official site of Spring Framework. Here, we are
explaining steps.

How to create simple Spring Boot application? @

To create an application. We are using STS (Spring Tool Suite) IDE and it includes the
various steps that are explaining in steps.

What is a Dispatcher Servlet @

 Dispatcher servlet is handling all the request.

 It determines which is the right controller to execute that request.


Who is configuring Dispatcher @

Spring Boot automatically configures a spring application based on dependencies present or


not present in the classpath as a jar, beans, properties, etc.

What does Dispatcher do @

Servlet Dispatcher takes an incoming URL and determines the appropriate handlers
(Controller classes) and views to be used for it (usually JSPs). When the DispatcherServlet
figures out what the view looks like, it sends that back to the client as the response. Finally,
the DispatcherServlet sends the Response Object back to the client via the Response
Object.

How Bean obj converted to JSON @

HttpMessageConvertersAutoConfiguration This message converter converts the message


automatically. It sets up the Jackson bean and the message converter. The
Jackson2ObjectMapper converts beans to JSON and JSON to beans.
Who is configuring error mapping @

It configures the basicErrorController, errorAttributes, ErrorMvcAutoConfiguration, and


DefaultErrorViewResolverConfiguration. It creates the default error page which is known as
Whitelabel Error Page.

What are the Spring Boot Annotations? @


@SpringBootApplication

We use this annotation to mark the main class of a Spring Boot application:

@SpringBootApplication

class VehicleFactoryApplication {

public static void main(String[] args) {

SpringApplication.run(VehicleFactoryApplication.class, args);

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and


@ComponentScan annotations with their default attributes.

@EnableAutoConfiguration

@EnableAutoConfiguration, as its name says, enables auto-configuration. It means that


Spring Boot looks for auto-configuration beans on its classpath and automatically applies
them.

Note, that we have to use this annotation with @Configuration:


@Configuration

@EnableAutoConfiguration

class VehicleFactoryConfig {}

Auto-Configuration Conditions @

Usually, when we write our custom auto-configurations, we want Spring to use them
conditionally. We can achieve this with the annotations in this section.

We can place the annotations in this section on @Configuration classes or @Bean methods.

In the next sections, we’ll only introduce the basic concept behind each condition. For
further information, please visit this article.

@ConditionalOnClass and @ConditionalOnMissingClass

Using these conditions, Spring will only use the marked auto-configuration bean if the class
in the annotation’s argument is present/absent:

@Configuration

@ConditionalOnClass(DataSource.class)

class MySQLAutoconfiguration {

//...

@ConditionalOnBean and @ConditionalOnMissingBean

We can use these annotations when we want to define conditions based on the presence or
absence of a specific bean:

@Bean

@ConditionalOnBean(name = "dataSource")

LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// ...

@ConditionalOnProperty

With this annotation, we can make conditions on the values of properties:

@Bean

@ConditionalOnProperty(

name = "usemysql",

havingValue = "local"

DataSource dataSource() {

// ...

@ConditionalOnResource

We can make Spring to use a definition only when a specific resource is present:

@ConditionalOnResource(resources = "classpath:mysql.properties")

Properties additionalProperties() {

// ...

@ConditionalOnWebApplication and @ConditionalOnNotWebApplication

With these annotations, we can create conditions based on if the current application is or
isn’t a web application:

@ConditionalOnWebApplication

HealthCheckController healthCheckController() {

// ...
}

@ConditionalExpression

We can use this annotation in more complex situations. Spring will use the marked
definition when the SpEL expression is evaluated to true:

@Bean

@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")

DataSource dataSource() {

// ...

@Conditional

For even more complex conditions, we can create a class evaluating the custom condition.
We tell Spring to use this custom condition with @Conditional:

@Conditional(HibernateCondition.class)

Properties additionalProperties() {

//...

}.

@RestController @

The @RestController annotation is used to define the RESTful web services.


 It serves JSON, XML and custom response.
 It adds @Controller and @ResponseBody annotations to the class.
 We need to import org.springframework.web.bind.annotation package in our file, in
order to implement it.
@RestController
public class ProductServiceController {
}

@RequestMapping @

The @RequestMapping annotation is used to provide routing information. It tells to the


Spring that any HTTP request should map to the corresponding method. We need to import
org.springframework.web.annotation package in our file.
The HTTP method parameter has no default. So, if we don't specify a value, it's going to
map to any HTTP request.

@RequestBody @

The @RequestBody annotation is used to define the request body content type.

public ResponseEntity<Object> createProduct (@RequestBody Product product) {


}

@PathVariable @

The @PathVariable annotation is used to define the custom or dynamic request URI. The


Path variable in request URI is defined as curly braces {}

public ResponseEntity<Object> updateProduct (@PathVariable(“id”) String id) {


}

@RequestParam @

The @RequestParam annotation is used to red the request parameters from the Request


URL. By default, it is a required parameter. We can also set default value for request
parameters.

public ResponseEntity<Object> getProduct (


@RequestParam (value = “name”, required = false, defaultValue = “honey”)
String id) {
}

Process to Build a RESTful Web Services @

I. Add the Spring Boot Starter to Maven: − For building RESTful Web Services, we need
to add the Spring Boot Starter Web dependency into the build configuration file. In
order to use Maven, we must add the dependency in our pom.xml file –

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

II. For Gradle we must use the dependency in our build.gradle file.

compile('org.springframework.boot:spring-boot-starter-web')

III. Spring is a web application framework based on Java. It provides tools and libraries
to create a complete cutomized web application.

What is Spring Boot Actuator? @

Spring Boot provides actuator to monitor and manage our application. Actuator is a tool
which has HTTP endpoints. when application is pushed to production, you can choose to
manage and monitor your application using HTTP endpoints.

What is thymeleaf? @

It is a server side Java template engine for web application. It's main goal is to bring
elegant natural templates to your web application.

It can be integrate with Spring Framework and ideal for HTML5 Java web applications.

How to use thymeleaf? @

In order to use Thymeleaf we must add it into our pom.xml file like:

1. <dependency>    
2. <groupId>org.springframework.boot</groupId>    
3. <artifactId>spring-boot-starter-thymeleaf</artifactId>    
4. </dependency>    
How to connect Spring Boot to the database using JPA? @

Spring Boot provides spring-boot-starter-data-jpa starter to connect Spring application


with relational database efficiently. You can use it into project POM (Project Object Model)
file.

How to connect Spring Boot application to database using JDBC? @


Spring Boot provides starter and libraries for connecting to our application with JDBC. Here,
we are creating an application which connects with Mysql database. It includes the following
steps to create and setup JDBC with Spring Boot.

What is Spring Boot dependency management? @

Spring Boot manages dependencies and configuration automatically. You don't need to
specify version for any of that dependencies.

Spring Boot upgrades all dependencies automatically when you upgrade Spring Boot.

What are the Spring Boot properties? @

Spring Boot provides various properties which can be specified inside our
project's application.properties file. These properties have default values and you can set
that inside the properties file. Properties are used to set values like: server-port number,
database connection configuration etc.

What are the Spring Boot Starters? @

Starters are a set of convenient dependency descriptors which we can include in our
application.

Spring Boot provides built-in starters which makes development easier and rapid. For
example, if we want to get started using Spring and JPA for database access, just include
the spring-boot-starter-data-jpa dependency in your project.

You might also like