Spring Boot Auto-configurationSpring Boot auto-configuration automatically configures the Spring application based on the jar dependencies that we have added. For example, if the H2 database Jar is present in the classpath and we have not configured any beans related to the database manually, the Spring Boot's auto-configuration feature automatically configures it in the project. We can enable the auto-configuration feature by using the annotation @EnableAutoConfiguration. But this annotation does not use because it is wrapped inside the @SpringBootApplication annotation. The annotation @SpringBootApplication is the combination of three annotations: @ComponentScan, @EnableAutoConfiguration, and @Configuration. However, we use @SpringBootApplication annotation instead of using @EnableAutoConfiguration. @SpringBootApplication=@ComponentScan+@EnableAutoConfiguration+@Configuration When we add the spring-boot-starter-web dependency in the project, Spring Boot auto-configuration looks for the Spring MVC is on the classpath. It auto-configures dispatcherServlet, a default error page, and web jars. Similarly, when we add the spring-boot-starter-data-jpa dependency, we see that Spring Boot Auto-configuration, auto-configures a datasource and an Entity Manager. All auto-configuration logic is implemented in spring-boot-autoconfigure.jar, as shown in the following figure. ![]() Need of auto-configurationSpring-based application requires a lot of configuration. When we use Spring MVC, we need to configure dispatcher servlet, view resolver, web jars among other things. The following code shows typical configuration of a dispatcher servlet in a web application: Similarly, when we use Hibernate/ JPA, we need to configure datasource, a transaction manager, an entity manager factory among a host of other things. Configuring datasource Configuring entity manager factory Configuring transaction manager Disable Auto-configuration ClassesWe can also disable the specific auto-configuration classes, if we do not want to be applied. We use the exclude attribute of the annotation @EnableAutoConfiguration to disable the auto-configuration classes. For example: We can use the attribute excludeName of the annotation @EnableAutoConfiguration and specify the qualified name of the class, if the class is not on the class path. We can exclude any number of auto-configuration classes by using the property spring.autoconfigure.exclude. Spring Boot Auto-configuration ExampleIn the following example, we will see how Spring Boot's auto-configuration features work. Step 1: Open spring Initializr https://start.spring.io/. Step 2: Provide the Group name. We have provided com.w3cschoool. Step 3: Provide the Artifact Id. We have provided spring-boot-autoconfiguration-example. Step 4: Add the dependencies: Spring Web, Spring Data JPA, an H2 Database. Step 5: Click on the Generate button. When we click on the Generate button, it wraps the project in Jar file and downloads it to the local system. ![]() Step 6: Extract the Jar file and paste it into the STS workspace. Step 7: Import the project folder into STS. File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-autoconfiguration-example -> Finish It takes some time to import. Step 8: Create a package with the name com.w3cschoool.controller in the folder src/main/java. Step 9: Create a Controller class with the name ControllerDemo in the package com.w3cschoool.controller. ControllerDemo.java Step 10: Create another package with the name com.w3cschoool.model in the folder src/main/java. Step 11: Create a class with the name User in the package com.w3cschoool.model. User.java Now we need to Configure the H2 database. Step 12: Open the application.properties file and configure the following things: port, enable the H2 console, datasource, and URL. application.properties Step 13: Create a SQL file in the folder src/main/resources. Right-click on the folder src/main/resources -> New -> File -> Provide the File name -> Finish We have provided the file name data.sql and insert the following data into it. data.sql Step 14: Create a folder with the name webapp in the src folder. Step 15: Create a JSP file with the name that we have returned in the ControllerDemo. In the ControllerDemo.java, we have returned home.jsp. home.jsp Step 16: Run the SpringBootAutoconfigurationExampleApplication.java file. We can see in the console that our application is successfully running on port 8085. ![]() Step 17: Open the browser and invoke the URL http://localhost:8085/h2-console/. It shows the Driver Class, JDBC URL that we have configured in the application.properties file, and the default User Name sa. ![]() We can also test the connection by clicking on the Test Connection button. If the connection is successful, it shows a message Test Successful. Step 18: Click on the Connect button. It shows the structure of the table userdata that we have defined in the User.java file. ![]() Step 19: Execute the following query to see the data that we have inserted in the data.sql file. ![]() Let's have a close look at the console. We see that TransactionManagement, DispatcherServlet, EntityManagerFactory, and DataSource automatically configures, as shown in the following figure. ![]() Debugging Auto-configurationWe can find more information about auto-configuration by using the following two ways:
Turning on debug logging We can debug logging by adding a property in the application.properties file. Let's implement the debug logging in the above example. Open the application.properties file and add the following property: Now restart the application. We see that an auto-configuration report printed in the log. The report includes all the classes that are auto-configured. It is divided into two parts: positive matches annegative matches, as shown in the following figure. Positive matches ![]() Negative matches ![]() Spring Boot Actuator We can also debug auto-configuration by using Actuator in the project. We will also add in HAL Browser to make things easy. It shows the details of all the beans that are auto-configured, and those are not. Let's create an example of Spring Boot Actuator. Step 1: Open Spring Initializr http://start.spring.io. Step 2: Provide the Group name. We have provided com.w3cschoool. Step 3: Provide the Artifact Id. We have provided actuator-autoconfiguration-example. Step 4: Add the dependencies: Spring Web and Spring Boot Actuator. Step 5: Click on the Generate button. It binds all the specification related to project into a jar file and downloads it in our local system. Step 6: Extract the downloaded jar file. Step 7: Import the project folder by using the following steps: File -> Import -> Existing Maven Project -> Next -> Browse -> Select the Project folder -> Finish After importing the project, we can see the following directory structure in the Package Explorer section of the IDE. ![]() Step 8: Create a Controller class in the package com.w3cschoool. We have created a controller class with the name DemoRestController. In the Controller, we have defined a method named hello() that returns a string. DemoRestController.java Step 9: Run the ActuatorAutoConfigurationExampleApplication.java file. Step 10: Open the browser and invoke the URL http://localhost:8080/hello. It returns a string that we have specified in the controller. ![]() Now invoke the actuator URL http://localhost:8080/actuator. It launches the actuator that shows the three URLs: self, health, and info, as shown below. Step 11: Open the pom.xml file and add the HAL Browser dependency. Step 12: Again, run the ActuatorAutoConfigurationExampleApplication.java file. To access the HAL browser, type http://localhost:8080 in the browser and hit the enter key. ![]() Now we can access the actuator through the HAL browser. Type /actuator in the Explorer's text box and click on the Go button. ![]() It shows all the things related to the actuator. The most important thing in the actuator is beans. ![]() When we click on the bean's arrow, it shows all the beans configured in the Spring Boot project. ![]() The above image shows the details of all the beans that are auto-configured and that are not. Next TopicSpring Boot Hello World Example
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com