深入浅析Spring-boot-starter常用依赖模块

时间:2019-04-13
本文章向大家介绍深入浅析Spring-boot-starter常用依赖模块,主要包括深入浅析Spring-boot-starter常用依赖模块使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring-boot的2大优点:

1.基于Spring框架的“约定优先于配置(COC)”理念以及最佳实践之路。

2.针对日常企业应用研发各种场景的Spring-boot-starter自动配置依赖模块,且“开箱即用”(约定spring-boot-starter- 作为命名前缀,都位于org.springframenwork.boot包或者命名空间下)。

应用日志和spring-boot-starter-logging

常见的日志系统大致有:java.util默认提供的日志支持,log4j,log4j2,commons logging,下面的spring-boot-starter-logging也是其中的一种。

maven依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-logging</artifactId>
  </dependency>

springBoot将使用logback作为应用日志的框架,程序启动时,由org.springframework.boot.logging-Logging-Application-Lisetener根据情况初始化并使用。

如果要想改变springBoot提供的应用日志设定,可以通过一下原则:

遵循logback的约定,在classpath中使用自己定制的logback.xml配置文件。

在文件系统的任意一个位置提供自己的logback.xml配置文件,然后通过logging.config配置项指向这个配置文件然后引用它,例如在application.properties中指定如下的配置:

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

快速web应用开发与spring-boot-starter-web

maven依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

在当下项目运行mvn spring-boot:run就可以直接启用一个嵌套了tomcat的web应用。

如果没有提供任何服务的Cotroller,访问任何路径都会返回一个springBoot默认的错误页面(Whitelabel error page)。

嵌入式Web容器层面的约定和定制

spring-boot-starter-web默认使用嵌套式的Tomcat作为Web容器对外提供HTTP服务,默认端口8080对外监听和提供服务。

我们同样可以使用 spring-boot-starter-jetty 或者 spring-boot-starter-undertow 作为Web容器。

想改变默认的配置端口,可以在application.properties中指定:

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

类似的配置还有:

server.address
server.ssl.*
server.tomcat.*

如果上诉仍然没有办法满足要求,springBoot支持对嵌入式的Web容器实例进行定制,可以通过向IoC容器中注册一个EmbeddedServletContainerCustomizer类型的组件来对嵌入式的Web容器进行定制

public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
    public void customize(ConfigurableEmbeddedServletContainer container){
      container.setPort(9999);
      container.setContextPath("C\\hello");
              ...
    }
  }

数据访问与spring-boot-starter-jdbc

maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>

默认情况下,当我们没有配置任何DataSource,SpringBoot会为我们自动配置一个DataSource,这种自动配置的方式一般适用于测试,开发还是自己配置一个DataSource的实例比较好。

如果我们的工程只依赖一个数据库,那么,使用DataSource自动配置模块提供的参数是最方便的:

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

还会自动配置的有:JdbcTemplate DateSourceTransactionManager等,我们只要在使用的时候注入(@Autowired)就好了

此外,SpringBoot还支持的数据库有spring-boot-data-jpa spring-boot-data-mongodb

spring-boot-starter-aop应用及其使用场景

AOP:Aspect Oriented Programming,面向切面编程

maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>

spring-boot-starter-aop主要由2部分组成:

1.位于spring-boot-autoconfigure的org.sringframework.boot.autoconfigure.aop.AopAutoConfiguration提供的@Configuration配置类和相应的配置项,即下面的2个配置项:

spring.aop.auto=true
spring.aop.proxy-target-class=false

2.spring-boot-starter-aop模块提供了针对spring-aop aspectjrt 和aspectjweaver的依赖

应用安全与spring-boot-starter-security //todo

总结

以上所述是小编给大家介绍的Spring-boot-starter常用依赖模块,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!