sa Flashcards
What is Spring boot?
Sprint boot is an extension of spring based framework used to quickly create Application
It has extra support of auto-configuration and embedded application server like tomcat, jetty, Undertow
What are the advantages of using Spring Boot?
1- Less configuration
2- Increases productivity and reduces development time by less amounts of source code;
3- don’t need to write any XML configuration, only a few annotations are required to do the configuration.
4- Helping to directly embed Tomcat, Jetty, or Undertow into an application;
What are the Spring Boot key components?
1- auto-configuration.
2- CLI.
3- starter POMs.
4- Actuators. (use to monitor our application)
Why Spring Boot over Spring
1- Auto Configuration. 2- Started' POMs 3- Actuators 4- Component Scanning. 5- Embedded server. 6- InMemory DB. 7- Externalized Configuration
we need not to specify the version of the dependencies in our configuration. Spring Boot manages itself. Spring Boot upgrades all dependencies automatically
@Bean
@Bean is a method-level annotation
Used to explicitly declare a single bean in a configuration class.
The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
Java based configuration: If you want to use a java based configuration then you have to use @Configuration annotation over your class which you will use to load the container. Now for the bean definition, you can do with in two.
a)In your configuration class @Bean annotations over the factory methods which you give you your bean objects by using the new keyword. and you have to add @Configuration over the class name
@Bean public HelloWorld helloWorld(){ return new HelloWorld(); } b) @Component/@Service/@Repository/@Controller/@RestController over the class which you want to act as a bean.
How does Spring Boot works?
1- The entry point of the spring boot application is the class that contains @SpringBootApplication annotation and the main method. spring boot has a main() and it calls static run()
2- Automatically scans all the components included in the project by using @ComponentScan annotation initialization the beans and package declarations
3- Automatically configures your application based on the dependencies you have added to the project by using annotation.
4-Spring Starter Dependencies (POM) to ensure that your application starts with the correct dependencies
What are starter dependencies?
Starter POMs. is a maven template that contains a collection of all the relevant dependencies that are needed to start a particular functionality.
What’s the Difference Between Spring Boot and Spring MVC?
Spring MVC requires
- used for multi page application
- manual build configurations,
- use an application server.
Spring Boot
- use for singel page application
- auto configuration
- embedded tomcat server
@Controller
- The @Controller is a class-level annotation.
- The @Controller annotation marks a class as a Spring MVC Controller
- which allow the use of handler mapping annotations.
- 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.
@Controller public class BooksController { @RequestMapping("/computer-science/books") public String getAllBooks(Model model) { //application code return "bookList"; }
Stereotype Annotations
the stereotype annotations in spring are a number of built in annotations @Component, @Service, @Repository, and @Controller. @Component is a class-level annotation. It is used to indicate, a class as a Component. We can use @Component across the application to mark the beans as Spring's managed components
Spring will automatically import the beans into the container and inject to dependencies. … These annotations are called Stereotype annotations as well.
@Component
It is a class-level annotation
It means that Spring framework will autodetect these classes for dependency injection when annotation-based configuration and class path scanning is used.
@Service , @Repository , @Controller(MVC) already have embedded @Component
@Component is a generic stereotype for any Spring-managed component.
@Repository
It is a class-level annotation. The @Repository annotation marks a class to be used as repository DAOs (Data Access Object) that access the database directly. use all the operations related to the database.
@Service
It is a class level annotation. it marks a class as a Service for an application It tells the Spring that class contains the business logic.
@Configuration
the @Configuration indicates that the class can be used by the Spring IoC (Inversion of Control) container as a source of bean definitions.
@Configuration public class AppConfig {
@Bean(name="demoService") public DemoClass service() { } }
@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 release because developers provided an alternative of the annotation, i.e. @SpringBootApplication.
@Required Annotation
Spring @Required annotation is applicable to bean property setter methods
@Required
public void setName(String name) {
this.name = name;
@Autowired
It is used for dependency injection.
In spring boot application, all loaded beans are eligible for auto wiring to another bean.
The annotation @Autowired in spring boot is used to auto-wire a bean into another bean.
it can be applied to a constructor, field, or setter method it helps to autowire the bean without creating an object using new keyword
@Component public class Customer { private Person person; @Autowired public Customer(Person person) {
Spring Boot Actuator
It is a Spring library that used to manage and monitor production operations.
like endpoint URL mapping, beans, logging information, health check Matrics without we need to write too much code
To include Actuator in your Spring Boot project simply add it to the pom.xml
by default it is disable to make it active you have to do from application. Property
@PathVariable:
It is used to extract the values from the URI path
it is used in method argument to bind it to the value of the URI templet variable
We can define multiple @PathVariable in a method.
@Configuration:
It is a class-level annotation. The class annotated with @Configuration used by Spring Containers as a source of bean definitions.
@Configuration public class Vehicle { @BeanVehicle engine() { return new Vehicle(); } }
@ComponentScan:
It is used when we want to scan a package for beans.
We can also specify the base packages to scan.
@ComponentScan(basePackages = “com.javatpoint”)
It is used with the annotation @Configuration.
@ComponentScan @Configuration public class ScanComponent { // ... }
@Component
It is a class-level annotation.
@Component is an annotation that allows Spring to automatically detect our custom beans.
It is used to mark a Java class as a bean. So you can Autowired 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.
@SpringBootApplication:
It is a combination of three annotations :
@EnableAutoConfiguration, (stop any configuration found in any other classes in the starter dependencies)
@ComponentScan, (scan all packages components(@Component, @Service, @Repository) inside the base package)
@SpringBootConfiguration (contain inside it @Configuration ) to provide a configuration as java base configuration
Types of configuration
1- xml configuration
2- java base configuration
3- Annotations configuration @Configuration
i.e we can create class configuration and add the @Configuration and @Bean to configure anything
@GetMapping:
It maps the HTTP GET requests on the specific handler method matched with given URI expression.
It is used to create a web service endpoint that fetches
Acts as a shortcut instead of using:
@RequestMapping(method = RequestMethod.GET)
@PostMapping:
It maps the HTTP POST requests on the specific handler method.
It is used to create a web service endpoint that insert
It is used instead of using: @RequestMapping(method = RequestMethod.POST)
@PutMapping:
It maps the HTTP PUT requests on the specific handler method.
It is used to create a web service endpoint that creates or updates
It is used instead of using: @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping:
It maps the HTTP DELETE requests on the specific handler method.
It is used to create a web service endpoint that deletes a resource.
It is used instead of using: @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping:
It maps the HTTP PATCH requests on the specific handler method.
It is used instead of using: @RequestMapping(method = RequestMethod.PATCH)
@RequestBody:
It is used to bind HTTP request body with an object in a method parameter.
Internally it uses HTTP MessageConverters to convert the body of the request.
@ResponseBody:
It binds the method return value to the response body.
It tells the Spring Boot Framework to serialize a return an object into JSON and XML format.
@RequestParam:
It is used to extract values from the query string URI after ?
(localhost:8080/employees?id=20)
It is also known as a query parameter.
It is most suitable for web applications.
It can specify default values if the query parameter is not present in the URL.
@RequestHeader:
It is used to get the details about the HTTP request headers.
We use this annotation as a method parameter.
The optional elements of the annotation are name, required, value, defaultValue.
For each detail in the header, we should specify separate annotations. We can use it multiple time in a method
@RestController:
It is a combination of
@Controller
@ResponseBody
It ignore the need for annotating each method with @ResponseBody.
@RequestAttribute:
It binds a method parameter to request attribute.
It provides convenient access to the request attributes from a controller method. With the help of @RequestAttribute annotation,
we can access objects that are populated on the server-side.
@AspectJ support
@AspectJ refers to a style of declaring aspects through the use of regular Java classes with annotations.
@AspectJ style was introduced in the AspectJ 5 release of the AspectJ Project. Note that Spring interprets the same annotations as AspectJ 5, using a library supplied by AspjectJ, however Spring’s AOP runtime is not dependent on the AspectJ compiler or weaver.
@Transactional
JPA on itself does not provide any type of declarative transaction management.
When using Spring and Spring ORM or Spring Data your chosen ORM can be configured to use a transaction manager that will automatically handle transaction propagation, and isolation, commit, and rollback.
use the @Transaction annotation above the serviceimpl class so it will give the default configuration of transaction
DevOps
Software Development (dev) Operations (ops)
Are a set of practices and methodologies use to combine the development, deployment and maintenance of code into a streamlined process.
The primary goal of DevOps is to accelerat the lifecycle of application development, particularly through the automation of tasks.
AOP Aspect Oriented Programming and Cross Cutting Concerns
Aspect-Oriented Programming supplements Object-Oriented Programming it is pattern to increase modularity. ( improves the quality of a specific piece of code.)
AOP breaks program logic into various parts, which are called cross-cutting concerns.
What is cross-cutting concerns in Spring boot?
is a part of a program that rely on or must affect many other parts of the system
These cross-cutting concerns are different from the main business logic like Database, Security, data entities, error handling, Transaction Management. or logging system messages. so we can add additional behavior to the existing code without modification of the code itself just at runtime. it is reuse and you can Enable or disable