What is Spring boot?
Spring, a notable Java-based framework, is used for developing enterprise and web applications. In contrast to many other popular frameworks that lay focus on a singular area, the Spring boot framework is known to provide a wide range of features that are capable of addressing diverse modern business needs. The portfolio projects of spring boot framework are flexible enough to configure beans in different manners such as XML, JavaConfig and Annotations.
What are various Spring boot Advantages?
The benefits of spring boot are:
- It is quite easy to build spring boot-based applications with Groovy or Java.
- It enhances productivity and reduces the development time to a very large extent.
- With spring, developers can avoid the writing of multiple/ repetitive and boilerplate annotations, codes and XML configurations.
- The Spring Boot Application can be easily integrated with the other parts of the spring eco-system like Spring ORM, Spring Security, Spring JDBC, Spring Data and so forth.
- Spring follows the “Opinionated Defaults Configuration” principles for reducing the efforts of a developer.
- It offers embedded HTTP servers of the likes of Jetty, Tomcat, etc. for the easy development and testing of web applications.
- Spring boot offers the features of CLI (Command Line Interface) for the development and testing in Java or Groovy applications. The needful can be carried out from the command prompt quite easily and quickly.
- It offers numerous plugins for developing and testing the applications created in Spring Boot with the help of specialized tools like Maven, Gradle, etc.
- The plug-ins offered by spring boot are designed to work along with in-memory and embedded databases.
What is application.properties in spring boot?
application.properties refers to a file extension for files that are primarily utilized by Java related technologies such as spring boot. It is used for the storage of the configurable parameters of any given application. Additionally, application.properties can be utilized for the storage of strings for the purpose of localization and internationalization; herein, they are referred to as Property Resource Bundles.
How to change the port in spring boot?
Spring boot presents a flexible means to help developers go about the configuration of their applications with the aid of property files. In order to change the port that has been set as the default port, the port number so desired is to be set up by the developer by accessing the properties of server.port. Either the application.properties file or the <application.yml can be used for making changes to the default port.
How to create a spring boot application?
- As most IDEs support Maven, it is common for developers to use the same for creating spring boot applications. The steps are:
- Open a terminal and ensure that the valid versions of Maven and Java are installed by running a specific code.
- Go to start.spring.io and select the "Web" starter option from the provided dependencies searcher.
- A new project structure is created to help the coding process. The Spring Initializr documentation can be referred to for more details.
- The code has to be created in a suitable folder that is the current directory.
- Create a Maven pom.xml file to build the project.
- The coding for the project can be done in any text editor of preference.
- Once the working build is ready, it is to be tested by running the mvn package.
- The project can be imported into a Java IDE that provides support for Maven at this stage.
- Add the desired classpath dependencies; Spring Boot offers several “Starters” to help developers add jars to their desired classpath.
- In order to finish the spring boot application, a single Java file has to be created. Maven, by default, compiles sources attributed to src/main/java. Just build the folder structure and add a file called src/main/java/Example.java for containing the specific code.
- The @RequestMapping and @RestController Spring MVC annotations can be used for the configeration of the application as desired by the project.
- Though Spring Boot auto-configures “Starter” applications, jar dependencies can be chosen outside of the starters to enable the process.
- The main method has to be called to delegate the created application to Spring Boot’s SpringApplication class. The application is bootstrapped by SpringApplication that starts the Tomcat web server that has already been auto-configured.
- Run the appliaction by using the spring-boot-starter-parent POM.
How to debug spring boot application?
The Maven plugin should be installed in Eclipse for running the debugging process smoothly. The steps to be followed are:
- Initiate the Tomcat server instance in Eclipse.
- Open the Debug view after the server instance starts.
- Ensure that the application’s running threads are shown as a hierarchical tree-like structure in the view window.
- Look for the application’s name at the top of the hierarchy.
- The application name should be depicted in the Debug view even after the Tomcat server instance is stopped.
- Choose the application name and right click on it.
- Select Edit Source Lookup.
- Select the desired application root directory where Eclipse should be looking for the source code.
- Restart the Tomcat server instance.
- Once the instance shows up, it will be possible for you to debug the source code in spring boot.
What are the Spring Boot Annotations?
The Spring framework, which is an IOC container, promotes and implements the principles of dependency injection (DI) and control inversion (IOC). It allows developers to manage bean dependencies via the use of XML-based configuration. These beans and their dependencies can also be defined through a Java-based configuration method that uses Spring annotations. In contrast to the XML approach, this Java-based configuration approach creates programs for the management of bean components.
Explain different types of Spring boot annotations?
The different Spring boot annotations are as follows:
It is used for marking the main class of any Spring Boot. The @SpringBootApplication is known to encapsulate @EnableAutoConfiguration, @Configuration and the @ComponentScan annotations along with their default properties and attributes.
@EnableAutoConfiguration paves the ground for auto-configuration. Spring Boot searches for auto-configuration beans on the classpath of @EnableAutoConfiguration and applies them automatically.
@ConditionalOnMissingClass and @ConditionalOnClass
When these conditions are used, Spring uses the marked auto-configuration beans in case the class representing the argument in the annotation’s argument is either present or absent.
@ConditionalOnMissingBean and @ConditionalOnBean
These annotations are useful for defining conditions on the basis of the absence or presence of specific beans.
This spring boot annotation allows developers to make conditions based on the values of properties.
This spring boot annotation allows the use of a definition in case a specific resource is available.
@ConditionalOnNotWebApplication and @ConditionalOnWebApplication
These annotations help in the creation of conditions on the basis of whether a particular application is web-based or not.
This spring boot application is useful in more complex situations that are true/ false on the basis of given conditions.
A class that evaluates custom conditions in complex conditions uses the annotation @Conditional.
What is Spring boot actuator?
Spring Boot Actuator provides additional features for monitoring and managing applications when pushed to production by developers. HTTP or JMX end points may be used for this purpose.
How does spring boot actuator work?
Spring Boot incorporates several additional features for monitoring and managing applications when pushed to production via JMX or HTTP endpoints. Health, auditing and metrics gathering may be automatically applied to applications. Several production-ready features attributed to Spring Boot are offered by the spring-boot-actuator. The easiest means of enabling the features is by adding a specific dependency ‘Starter’ termed spring-boot-starter-actuator.
What is spring boot in java?
Spring Boot, launched by the team at Pivotal, is a framework designed for simplifying bootstrapping and the development of new Spring applications. It showcases an opinionated approach towards configuration and frees developers from the process of defining a boilerplate configuration.
What is spring boot transaction management?
Transaction management serves to be a significant part of RDBMS-oriented enterprise applications. It ensures consistency and data integrity. An abstract layer is provided by the
Spring framework on top of various transaction management APIs. The transaction support system of Spring is aimed at providing an alternative to numerous EJB transactions. It does so by adding different transaction capabilities to POJOs. Both declarative transaction management and programmatic transaction management are supported by spring. Contrary to EJBs that necessarily require application servers, Spring transaction management is capable of being implemented even without an application server.
What is @restcontroller in spring boot?
@RestController was introduced by Spring 4.0 as a specialized version and convenience-based annotation of the controller. It is responsible for adding the @ResponseBody and @Controller annotations. When the controller class is annotated with @RestController annotation, the @ResponseBody annotation may not be included in the request mapping methods. Do note that the @ResponseBody annotation will be active by default.
What is @requestmapping in spring boot?
@RequestMapping refers to the commonest annotation in the repository of Spring Web applications. It maps HTTP requests for handling the methods of REST and MVC
What is @qualifier in spring boot?
The @Qualifier annotation is used in situations wherein more than a singular type of bean is created and only one needs to be wired with a property. The @Qualifier annotation can be used along with @Autowired for removing the confusion and specifying the exact beans that have to be wired.
How to Deploy Spring Boot WAR to Tomcat?
Say, you want to deploy and run a WAR file in Tomcat. The following steps will help your cause:
- Download Apache Tomcat.
- Unpackage the same into the desired tomcat folder.
- Copy the WAR file from the target/spring-boot-tomcat.war into the tomcat/webapps/ folder.
- From a terminal, go to tomcat/bin folder.
- Execute the file.
What is JPA?
Created to serve as an integral part of EJB 3 specification, JPA is an important Java object-relational persistence framework that is very useful for mapping processes. While other covered frameworks are said to be standards-based, JPA is in itself a standard in Java Persistence API.
How to use JPA in Spring boot?
In spring boot data, JPA lays focus on utilizing JPA for the storage of data in relational databases. One of its most compelling features relates to the creation of repository implementations automatically. It does so at runtime and from a repository interface. Spring Data JPA is capable of creating an implementation when an application is made to run.
What is CORS in Spring Boot?
Cross-origin resource sharing (CORS) refers to a W3C specification. It is implemented by commonly-used browsers and helps users specify flexible ways of authorizing cross domain requests. CORS serves as an alternative to less powerful and less secure hacks of the kinds of IFrame or JSONP.
How to enable CORS in Spring Boot?
The Spring Framework 4.2 GA offers support for CORS to offer better configuration than is possible through typical filter-based solutions.
The Controller method of CORS configuration
The @CrossOrigin annotation can be added to the @RequestMapping handler method for enabling CORS in spring boot. @CrossOrigin allows all the HTTP methods and origins specified in @RequestMapping annotation (by default) for this purpose.
Global CORS configuration
When a global CORS configuration needs to be defined along with annotation-based, fine-grained configurations, then the same has to be declared in Spring MVC and further combined with @CrossOrigin configuration via filters. The GET, POST and HEAD methods, and all origins are allowed by default in this method of enabling CORS in spring boot.
What is Swagger?
Swagger refers to a specification in spring boot that is useful for documenting REST API. It is important for specifying the method, URL, representation and other formats for describing the REST web services. Swagger provides tools for generating /computing the documentation from various application codes.
How to add swagger to spring boot?
The following steps are helpful in adding swagger to spring boot:
1) Generation of swagger documentation with spring boot: A docket has to be configured and some dependencies linked to swagger have to be added for generating the swagger documentation. The Swagger UI provides visual representations of the documentation and helps in the execution of test requests. The Spring configuration required for generating Swagger Documentation is:
2) Viewing of the generated swagger documentation: Once the application is restarted, the swagger documentation can be viewed by going to URL http://localhost:8080/v2/api-docs. The Meta Information pertaining to the API can be found on top of the specific documentation.
What is docker and purpose of docker in spring boot?
A Docker is in the nature of a container tool that allows for easy deployment and running of applications using containers. These containers help developers create all-inclusive packages of the developed applications with the necessary dependencies. Providing a "social" aspect, Docker is a smartly designed Linux container management toolkit. It helps users publish container images and also use the ones that are published by others. Docker images provide helpful means for running containerized processes that are necessary for the building of spring boot applications.
What is Spring boot devtools?
The Spring Boot Dev Tools framework module was included in 1.3 for the sake of providing powerful development tools. These development tools shorten the overall development cycle to enable easy deployment as well as the testing processes carried out during development. The developer-friendly features of spring-boot-devtools can be added to a project once the build function is performed.
What are some advantages of using spring boot for building out microservices?
The Spring Boot advantages for building out microservices are as follows:
- Spring boot offers opinionated views of the Spring Platform as well as third-party libraries. Given this feature, developers can start with minimum efforts and get access to easy tools for creating standalone applications that are embedded with the properties of Tomcat/Jetty/Undertow.
- Spring boot provides production-ready services for building microservices.
- The spring boot actuator features basic health-check as well as monitoring functions that are useful for microservices.
- Spring boot offers the embedded, pre-configured, Tomcat server.
- It provides executable JAR packaging along with useful dependencies.
- Spring boot has minimal configuration overheads and can be customized for meeting the development needs of microservices.
How to use h2 database in Spring boot?
The following steps have to be followed for using H2 in spring boot:
- Go to https://start.spring.io/
- Click on the button tagged as Generate Project after filling the artifactId, Group and dependencies (web /H2).
- A zip file named springBootAndH2DbIntegration will be downloaded on the local drive.
- Extract the zip.
- The extracted folder has to be imported in Eclipse as an existing Maven Project.
- Open the pom.xml file that has an entry for H2 database. The h2 dependency has to be added to link up with the H2 in-memory database. Look for the H2 jar on the classpath. Spring boot will automatically create a data source for connecting to the H2 database.
- Open springBootAndH2DbIntegrationApplicaiton.java.
- Run the above as a Java application.
- The embedded Tomcat Server will get initiated and the application will be deployed on Tomcat.
- The log will show that Tomcat has started on port 8080. Also, check if the SprigBootAndH2DbIntegrationApplication has started successfully or not.
- Open the web console of H2 with the following URL:
- It will show that the H2 web console is running and is ready for you to connect to the H2 database.
- Click on the Test Connection button and confirm whether the connection is successful or not.
- Click on the Connect button to check that the established database connection with h2 DB is ready for use.