In-depth analysis of common dependency modules of Spring-boot-starter

2 major advantages of Spring-boot:

1. Based on the concept of "Convention over Configuration (COC)" based on the Spring framework and the road to best practices.

2. Spring-boot-starter automatically configures dependent modules for various scenarios of daily enterprise application development, and "out-of-the-box" (convention spring-boot-starter- as the naming prefix, all located in the org.springframenwork.boot package or named space).

Application logging and spring-boot-starter-logging

Common log systems generally include: log support provided by java.util by default, log4j, log4j2, commons logging, and the following spring-boot-starter-logging is also one of them.

maven dependencies:

  1.  
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-logging</artifactId>
  5. </dependency>

springBoot will use logback as the framework for application logging. When the program starts, it is initialized and used by org.springframework.boot.logging-Logging-Application-Lisetener according to the situation.

If you want to change the application log settings provided by springBoot, you can use the following principles:

Follow the logback convention and use your own customized logback.xml configuration file in the classpath.

Provide your own logback.xml configuration file anywhere in the file system, then point to this configuration file through the logging.config configuration item and reference it, for example, specify the following configuration in application.properties:

  1.  
  2. logging.config=/{some.path.you.defined}/any-logfile-name-I-like.log}

 

Rapid web application development with spring-boot-starter-web

maven dependencies:

  1.  
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>

Running mvn spring-boot:run in the current project can directly enable a web application with nested tomcat.

If there is no Cotroller providing any services, accessing any path will return a springBoot default error page (Whitelabel error page).

Conventions and customizations at the embedded web container level

By default, spring-boot-starter-web uses nested Tomcat as the Web container to provide HTTP services to the outside world, and the default port 8080 monitors and provides services to the outside world.

We can also use  spring-boot-starter-jetty  or  spring-boot-starter-undertow  as a web container.

To change the default configuration port, specify in application.properties:

  1.  
  2. server.port = 9000(the port number you want)

Similar configurations are:

  1.  
  2. server.address
  3. server.ssl.*
  4. server.tomcat.*

If the appeal still fails to meet the requirements, springBoot supports the customization of the embedded Web container instance, and the embedded Web container can be customized by registering a component of type EmbeddedServletContainerCustomizer with the IoC container.

  1.  
  2. public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
  3. public void customize(ConfigurableEmbeddedServletContainer container){
  4. container.setPort(9999);
  5. container.setContextPath("C\\hello");
  6. ...
  7. }
  8. }

Data access with spring-boot-starter-jdbc

maven dependencies:

  1.  
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-jdbc</artifactId>
  5. </dependency>

By default, when we do not configure any DataSource, SpringBoot will automatically configure a DataSource for us. This automatic configuration method is generally suitable for testing. It is better to configure an instance of DataSource for development.

If our project only depends on one database, then it is most convenient to use the parameters provided by the DataSource auto-configuration module:

  1.  
  2. spring.datasource.url=jdbc:mysql://{datasource host}:3306/{databaseName}
  3. spring.datasource.username={database username}
  4. spring.datasource.passwd={database passwd}

It will also be automatically configured: JdbcTemplate DateSourceTransactionManager, etc. We just need to inject (@Autowired) when we use it.

In addition, the databases supported by SpringBoot are spring-boot-data-jpa spring-boot-data-mongodb

spring-boot-starter-aop application and its usage scenarios

AOP: Aspect Oriented Programming, Aspect Oriented Programming

maven dependencies:

  1.  
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-aop</artifactId>
  5. </dependency>

spring-boot-starter-aop mainly consists of 2 parts:

1. The @Configuration configuration class and corresponding configuration items provided by org.sringframework.boot.autoconfigure.aop.AopAutoConfiguration located in spring-boot-autoconfigure, namely the following two configuration items:

  1.  
  2. spring.aop.auto=true
  3. spring.aop.proxy-target-class=false

2. The spring-boot-starter-aop module provides dependencies for spring-aop aspectjrt and aspectjweaver

Application security and spring-boot-starter-security //todo

Summarize

The above is the common dependency module of Spring-boot-starter introduced by Xiaobian. I hope it will be helpful to you.
I have created a group to share technical dry goods, industry secrets from time to time, and bring together all kinds of wonderful and interesting topics. and popular trends! If you like my sharing, you can use QQ search 650385180 to add group attention. If you have any questions, please leave me a message and I will reply you in time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293578&siteId=291194637