国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java Javagetting Started What is the principle of spring boot?

What is the principle of spring boot?

Jun 16, 2020 pm 02:14 PM
spring boot

What is the principle of spring boot?

What is the principle of spring boot:

1. Content introduction

Introduced Spring Boot through "Spring Boot Experience" What can be done? This article mainly analyzes the basic implementation ideas of its various functional points, so as to have an overall rational understanding of Spring Boot.

  • Dependency management: Spring Boot has done a lot of starters, and starters are just an entrance to help us import dependencies, simplifying project dependency management

  • Automatic configuration: Spring Boot provides configuration classes for many common components and frameworks based on Spring code configuration, thereby simplifying project configuration

  • Embedded container: Common web containers that integrate Java to simplify development Environment construction, and it is the basis for packaging plug-ins to package web applications into executable files

  • Maven plug-in: used to package jar files or war files that can be run directly, for the out-of-box project Use to provide support, and of course there are some small functions to assist development

  • Hot start: Reduce the number of repeated starts of containers during the development process and improve development efficiency

  • Application Monitoring: Provides basic services for application auditing, health monitoring, and metric data collection

  • CLI (command line tool): for rapid prototyping, there is no need to use

2. Starter dependencies: starter

In a regular Maven project, if you want to use a certain framework or component, you need to import a large number of dependencies, and you must pay attention to the version matching of the dependencies, etc. The problem is that Spring Boot provides a large number of starters. Such starters will use Maven's dependency transfer to help us import related dependencies. For example, the following pom file will add web-related dependencies, including Spring Web, Spring MVC, etc.:

    .......    <dependencies>
        <!--Web應(yīng)用程序的典型依賴項-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

    </dependencies>
    .......

Principle: We just imported a dependency , but the Dependencies use Maven's dependency transfer to help us import a large number of packages used in web development. If we decompress the file corresponding to this dependency, we find the jar file In fact, there is no substantive content in it, because it is just a pom project. The substantive content is in the file corresponding to the package, which is created by mavne Downloaded when downloading the jar file, many dependencies are declared in the file, such as: spring-webmvc, spring-web, etc.

In short, if our project depends on a certain starter, then the starter will depend on many other dependencies, and Maven's dependency transfer will add the dependencies that the starter depends on to our project. . The starter is just an import intermediary for our project dependencies.

You can refer to relevant information about maven's dependency transfer. A brief description is as follows:

Project A depends on B, and B depends on C. Project A only needs to declare that it depends on B, but does not need to declare that it depends on C. Maven automatically manages the delivery of this dependency.

2. Automatic configuration: AutoConfiguration

Spring Boot will automatically configure related components or frameworks using default values ????according to certain conditions, thereby greatly reducing the project’s configuration files. It automatically scans and Added its own processing flow based on code configuration. The following content first briefly introduces Spring's code-based configuration, and then introduces what Spring Boot does.

(1) Spring code-based configuration

Before Spring3, the way to use Spring context was generally as follows:

Spring configuration file

<!-- 數(shù)據(jù)源 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
 <property name="username" value="hr" />
 <property name="password" value="hr" /></bean>
 <!--組件掃描-->
 <context:component-scan base-package="com.demo.spring.sample.step03.?rvic?" />

Business code

package com.demo.spring.sample.step03.service.impl;.......@Service("userService")
public class UserService implements IUserService {   
   @Autowired
   private IUserDAO userDAO = null;
}

Create Spring context

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Summary:

Tell Spring to scan the class path for @Component, @Repository, @Service, @ through component-scan For classes annotated with Controller, Spring instantiates these classes and registers the instances into the Spring context. However, third-party beans such as data sources and property files are still configured using XML files. If you want to completely eliminate XML configuration files, it is still not feasible. Yes, if it must be done, it is relatively troublesome.

There are advantages and disadvantages to using XML configuration files. One of the disadvantages is that you cannot customize the bean instantiation process too much. For example, for the above dataSource, you only need to instantiate it if you want to have Oracle's jdbc driver in the class path. ation, this cannot be accomplished. Another disadvantage is that the code logic is scattered, because part of the logic is configured in XML; of course, the advantage is that the configuration is centralized and it is convenient to switch configurations.

After Spring 3, you can use the Spring container in the following way:

Spring configuration class

@Configuration // 表明當(dāng)前類提供Spring配置文件的作用,Spring上下文會從當(dāng)前類的注解中提取配置信息
@ComponentScan(basePackages = "com.demo.spring.sample.step08") 
// 開啟組件掃描
public class AppConfig {
    @Bean // 表示這個方法實例化一個bean,id=dataSource
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }}

Business code

package com.demo.spring.sample.step03.service.impl;.......@Service("userService")
public class UserService implements IUserService {   
   @Autowired
   private IUserDAO userDAO = null;
}

Create Spring context

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

Summary:

XML-based configuration is no longer used when creating a Spring context. The configuration file disappears and is replaced by the AppConfig class. This class needs to be annotated with @Configuration, and this class can have @Bean annotated methods, the container will automatically call such methods to instantiate the Bean.

(2) Spring Boot's automatic configuration

Spring Boot has helped write a large number of classes annotated with @Configuration. Each class provides a component or framework configuration, such as DataSourceConfiguration. Static inner class Dbcp2 in .java, which provides configuration of DBCP data sources

//DBCP 數(shù)據(jù)源配置.
@Configuration //這個注解在實際代碼中沒有加,當(dāng)前類被其它配置類Import
@ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type",  matchIfMissing = true,havingValue = "org.apache.commons.dbcp2.BasicDataSource")
static class Dbcp2 {

   @Bean
   @ConfigurationProperties(prefix = "spring.datasource.dbcp2")
   public org.apache.commons.dbcp2.BasicDataSource dataSource(
         DataSourceProperties properties) {
      return createDataSource(properties,
            org.apache.commons.dbcp2.BasicDataSource.class);
   }

}

Summary:

當(dāng)Spring Boot啟動的基本步驟走完后, 如果啟用了自動配置,Spring Boot會加載下的<\META-INF\spring.factories>文件中的內(nèi)容,該文件中定義了有關(guān)自動配置的信息,其中EnableAutoConfiguration對應(yīng)的每一個類中都加入了注解@Configuration,也就是說這樣的每一個類都相當(dāng)于Spring的一個或多個配置文件,而其中加注解@Bean的方法是bean的工廠方法,會由Spring上下文自動調(diào)用,并且將返回的對象注冊到上下文中。

spring.factories文件中自動配置類的部分內(nèi)容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
......
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
......

(三)、如何覆蓋自動配置的屬性

Spring Boot的自動配置會采用大量的默認(rèn)值(約定由于配置),可以通過在類路徑下提供application.properties或者application.yml配置文件來覆蓋默認(rèn)值,當(dāng)然部分屬性值必須通過該配置文件來提供,比如如果要使用Spring Boot對數(shù)據(jù)源的自動配置,則在配置文件中必須提供jdbc的url,否則會拋出異常。

三、集成內(nèi)嵌容器 :main方法

Spring Boot支持的內(nèi)嵌容器Tomcat、Jetty、Undertow本身就支持在Java中內(nèi)嵌使用,因為這些容器本身就是使用Java編寫的,只不過Spring Boot在main方法的調(diào)用鏈中根據(jù)自動配置嵌入了這樣的容器。

不使用這樣的內(nèi)嵌容器也是可以的,在Maven中移除這樣的依賴就可以,當(dāng)然這個時候如果要通過Spring Boot使用Web相關(guān)框架,則需要打包為war包后獨立部署,或者在開發(fā)過程中使用IDE環(huán)境的開發(fā)部署功能。

不使用內(nèi)嵌容器的Web應(yīng)用在打包時需要對工程進(jìn)行一定的修改。

四、打包可運行文件 :maven-plugin

Maven使用的默認(rèn)打包工具支持打包jar文件或者war文件,但是打包后的jar文件中不能再嵌入jar文件,而打包后的war文件不能直接運行,為了把工程所有文件打包為一個可直接運行的jar文件或war文件,spring提供了一個maven插件來解決這樣的問題。當(dāng)然這個插件還提諸如spring-boot:run這樣的開發(fā)功能

五、熱啟動 :devtools

在開發(fā)過程中,當(dāng)完成一個功能單元后,我們會啟動程序查看運行效果,如果代碼需要再次修改,則修改后需要關(guān)掉程序,然后重啟程序,這個過程不斷迭代,從而完成代碼的編寫、調(diào)試。

Spring Boot 熱啟動通過重寫容器的類加載器,完成程序的部分重啟,從而簡化、加速程序的調(diào)試過程。spring-boot-devtools通過兩個類加載器分別加載依賴庫和項目代碼,當(dāng)spring-boot-devtools發(fā)現(xiàn)項目的編譯輸出路徑下有變化時,通過其中的一個類加載器重新加載所有的項目自有代碼,從而完成熱啟動。這樣的熱啟動比冷啟動(關(guān)閉、重啟)要快很多,到底快多少取決于項目自有代碼的數(shù)量。

和熱啟動對應(yīng)的還有一個熱替換,是指單獨地替換被修改的某一個class到j(luò)vm中,甚至可以單獨替換class的某個方法,這種方式比熱啟動要快,通常使用 JavaAgent 攔截默認(rèn)加載器的行為來實現(xiàn),spring有個獨立的項目Spring Loaded就是這么做的,但是項目已經(jīng)被移到了 attic 了,也就是被Spring束之高閣,所以不建議使用。

六、應(yīng)用監(jiān)控:actuator

如果類路徑中有actuator這個組件的話,Spring Boot的自動配置會自動創(chuàng)建一些端點(端點:遵循Restful設(shè)計風(fēng)格的資源,對應(yīng)于Controller的某一個處理請求的方法),這些端點接受請求后返回有關(guān)應(yīng)用的相關(guān)信息,比如:健康信息、線程信息等。返回的是json格式的數(shù)據(jù),而使用 Spring Boot Admin 可以實現(xiàn)這些 JSON 數(shù)據(jù)的視圖展現(xiàn),當(dāng)然也可以為其他應(yīng)用監(jiān)控系統(tǒng)監(jiān)控當(dāng)前系統(tǒng)提供服務(wù)。

七、問題:

為什么pom文件中要繼承spring-boot-starter-parent?

spring-boot-starter-parent是spring-boot提供的一個pom,在該pom中定義了很多屬性,比如:java源文件的字符編碼,編譯級別等,還有依賴管理、資源定義的相關(guān)pom配置,項目的pom如果繼承starter-parent,可以減少相關(guān)配置

推薦教程: 《java教程

The above is the detailed content of What is the principle of spring boot?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Spring Boot to build big data processing applications How to use Spring Boot to build big data processing applications Jun 23, 2023 am 09:07 AM

With the advent of the big data era, more and more companies are beginning to understand and recognize the value of big data and apply it to business. The problem that comes with it is how to handle this large flow of data. In this case, big data processing applications have become something that every enterprise must consider. For developers, how to use SpringBoot to build an efficient big data processing application is also a very important issue. SpringBoot is a very popular Java framework that allows

Build desktop applications using Spring Boot and JavaFX Build desktop applications using Spring Boot and JavaFX Jun 22, 2023 am 10:55 AM

As technology continues to evolve, we can now use different technologies to build desktop applications. SpringBoot and JavaFX are one of the more popular choices now. This article will focus on how to use these two frameworks to build a feature-rich desktop application. 1. Introduction to SpringBoot and JavaFXSpringBoot is a rapid development framework based on the Spring framework. It helps developers quickly build web applications while providing a set of

How to use Spring Boot to build blockchain applications and smart contracts How to use Spring Boot to build blockchain applications and smart contracts Jun 22, 2023 am 09:33 AM

With the rise of digital currencies such as Bitcoin, blockchain technology has gradually become a hot topic. Smart contracts can be regarded as an important part of blockchain technology. SpringBoot, as a popular Java back-end development framework, can also be used to build blockchain applications and smart contracts. This article will introduce how to use SpringBoot to build applications and smart contracts based on blockchain technology. 1. SpringBoot and blockchain First, we need to understand some basic concepts related to blockchain. Blockchain

Spring Boot+MyBatis+Atomikos+MySQL (with source code) Spring Boot+MyBatis+Atomikos+MySQL (with source code) Aug 15, 2023 pm 04:12 PM

In actual projects, we try to avoid distributed transactions. However, sometimes it is really necessary to do some service splitting, which will lead to distributed transaction problems. At the same time, distributed transactions are also asked in the market during interviews. You can practice with this case, and you can talk about 123 in the interview.

Building an ESB system using Spring Boot and Apache ServiceMix Building an ESB system using Spring Boot and Apache ServiceMix Jun 22, 2023 pm 12:30 PM

As modern businesses rely more and more on a variety of disparate applications and systems, enterprise integration becomes even more important. Enterprise Service Bus (ESB) is an integration architecture model that connects different systems and applications together to provide common data exchange and message routing services to achieve enterprise-level application integration. Using SpringBoot and ApacheServiceMix, we can easily build an ESB system. This article will introduce how to implement it. SpringBoot and A

Spring Boot's task scheduling and scheduled task implementation methods Spring Boot's task scheduling and scheduled task implementation methods Jun 22, 2023 pm 11:58 PM

SpringBoot is a very popular Java development framework. It not only has the advantage of rapid development, but also has many built-in practical functions. Among them, task scheduling and scheduled tasks are one of its commonly used functions. This article will explore SpringBoot's task scheduling and timing task implementation methods. 1. Introduction to SpringBoot task scheduling SpringBoot task scheduling (TaskScheduling) refers to executing some special tasks at a specific point in time or under certain conditions.

Using WebSocket in Spring Boot to implement push and notification functions Using WebSocket in Spring Boot to implement push and notification functions Jun 23, 2023 am 11:47 AM

In modern web application development, WebSocket is a common technology for instant communication and real-time data transfer. The SpringBoot framework provides support for integrated WebSocket, making it very convenient for developers to implement push and notification functions. This article will introduce how to use WebSocket to implement push and notification functions in SpringBoot, and demonstrate the implementation of a simple real-time online chat room. Create a SpringBoot project First, we need to create a

Achieve multi-language support and international applications through Spring Boot Achieve multi-language support and international applications through Spring Boot Jun 23, 2023 am 09:09 AM

With the development of globalization, more and more websites and applications need to provide multi-language support and internationalization functions. For developers, implementing these functions is not an easy task because it requires consideration of many aspects, such as language translation, date, time and currency formats, etc. However, using the SpringBoot framework, we can easily implement multi-language support and international applications. First, let us understand the LocaleResolver interface provided by SpringBoot. Loc

See all articles