zoukankan      html  css  js  c++  java
  • spring boot 入门

    1  历史上在spirng1版本,全都是使用xml配置,繁琐,在jdk1.5以后java支持了注解,后来spring也开始使用注解,形成了db使用xml,业务service使用注解的统一习惯。在spring4以后,支持了java配置,springboot建议使用java配置,包括@Configuration(声明当前是一个配置类,类似xml文件)和@Bean(用在方法上,说明返回一个bean)。springboot思想是“习惯优于配置”,内置了很多习惯性配置,即自动配置。同时提供了起步依赖,包含了多种基础依赖包括spring-web,spring-mvc等等。

       总结起来,springboot的特点有二: 一是继承starter,约定优于配置;二 本身是一些库的集合,不是新的框架,无需管理各种库。spring使编码更简单,配置更简单,部署更简单,监控更简单。

        核心功能:

      (1)独立运行的spring项目,可以打成jar包,java -jar就可运行。

      (2)内嵌servlet容器,可以选择tomcat,jetty,undertow等,无需以war包形式部署。

      (3)提供starter,简化mvn配置,pom可以直接依赖spring-boot-starter-web,会自动引入多种依赖如hibernate,springmvc等。

      (4)自动配置spring bean。会对类路径下的jar包里的类自动配置bean,极大减少配置。

      (5)应用监控。基于http,ssh,telnet对项目进行运行时监控。

      (6)无代码和xml配置。spring4.x提倡java配置和注解配置结合,因此spring-boot可以实现不用spring-xml配置。

        spring-boot优点 

       (1)快速构建项目。

       (2)集成主流开发框架,无须额外配置(无需额外管理jar包和配置xml)。

       (3)项目独立运行,不需servlet容器(已经内置)。

              * 默认tomcat,可以换成jerry undertow,servlets filters listeners 都可以通过声明为bean来被容器注册,还可以配置相关属性:server.port等。

       (4)提供运行时监控。

       (5)提高了开发部署效率。

       (6)与云计算结合。

       *可以使用idea的spring initializr和下面的web来创建项目,在新项目里pom里有父依赖spring-boot-starter-parent,规定了各种版本和基础依赖。

       * 项目提供了xxApplication作为应用入口类,其中@SpringBootApplication是Sprnig Boot项目的核心注解,主要目的是开启自动配置;main方法作为程序入口;还可以手动加上@RestController,相当于@Controller+@ResponseBody,让类里的方法都用json输出。

      * application.properties,这个文件用于配置全局变量。也支持自定义文件分别保存属性。 

    2 springboot 常用模块

       (1)spring-boot 主库,为各模块提供支持,SpringApplication类,提供main()方法直接启动,嵌入web应用和容器(tomcat等)。

       (2)spring-boot-autoconfigure    基于claspath下的内容配置通用的大部分模块,@EnableAutoConfiguration 触发spring上下文自动配置,自动配置可能需要的bean。自动配置优先级低于用户自己定义的bean。

       (3)spring-boot-starters   一组预定义的依赖,添加不同类型的应用功能。

       (4)spring-boot-cli  可以编译和运行groovy源码,提供极为简化的代码开发。

       (5)spring-boot-actuator   对应用系统集成监控功能,可以查看配置和相关统计等。比如url访问查看额外配置,查看bean配置,查看最近访问的若干requet记录等。

       (6)spring-boot-loader  自定义的classloader,对打包文件格式进行了定义,允许构建可用java -jar执行的jar包。      

    3 基本配置

       (1) xxApplication是应用入口类,@SpringBootApplication是核心注解,是一个组合注解如下。

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan 

        主要是@SpringBootConfiguration(里面组合@Configuration) @EnableAutoConfiguration @ComponentScan 

        @Configuration  用@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean;但它们同样可以用在普通的spring项目中,而不是Spring Boot特有的,只是在spring用的时候,注意加上扫包配置。

       @EnableAutoConfiguration 依赖springboot在类路径中的查找,自动载入应用程序所需要的bean。@Enable***这种注解spring3.x就出现了,比如@EnableWebMvc,本质上通过@Import({DelegatingWebMvcConfiguration.class}) 来引入需要的bean。@EnableAutoConfiguration也使用import,

    @Import({EnableAutoConfigurationImportSelector.class}),EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类的loadFactoryNamesof()方法, SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件,spring.factories包含了自动配置列表。例如spring-boot-starter-tomcat引入了tomcat的依赖,所以EmbeddedServletContainerAutoConfiguration发现存在Tomcat.class就会注入TomcatEmbeddedServletContainerFactory来内置web容器。如下:

    @Configuration
    @ConditionalOnClass({Servlet.class, Tomcat.class})
    @ConditionalOnMissingBean(
    value = {EmbeddedServletContainerFactory.class},
    search = SearchStrategy.CURRENT
    )
    public static class EmbeddedTomcat {
    public EmbeddedTomcat() {
    }

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory();
    }
    }

      @ComponentScan 自动扫描@SpringBootConfiguration同级包以及包内的bean,所以建议入口类放在groupid+artificalid下面。

    *可以关闭特定的配置,使用exclude,例如@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)

        (2)    application.properties,保存全局属性,例如配置前缀端口啥的,支持yaml语言文件(后缀yml),例如正常版

        server.port=8081

        server.context-path=/test

        如果用yaml语言,就是

        server:

             port:8081

         context-path:/test

       (3)spring boot starter是核心starter,包含自动配置、日志和YAML。官方还有一些集成好的starter共44种,例如 

              spring-boot-starter-actuator  帮助监控和管理应用。

              spring-boot-starter-aop 支持面向方面的编程即AOP,包括spring-aop和AspectJ。

              spring-boot-starter-jetty 引入了Jetty HTTP引擎(用于替换Tomcat)。

       *如果一定要读取xml配置,例如 @ImportResource({"classpath:xxx.xml"})

    3 自动注入的条件注解种类 

      @EnableAutoConfiguration定位到spring.factories文件,自动配置里面的功能,自动配置列表里的每一项都用了条件注解,比如上面例子的@ConditionalOnClass({Servlet.class, Tomcat.class}),在类路径下有制定的类才会进行配置。条件注解的种类包括以下等等:

    @ConditionalOnBean 当容器里有指定bean的情况下   

    @ConditionalOnMissingBean 当容器里没有指定bean的情况下   

    @ConditionalOnClass  当类路径下(classpath)有指定的类的情况下

    @ConditionalOnMissingClass  当类路径下没有指定的类的情况下

    @ConditionalOnExpression 基于el表达式进行判断

    @ConditionalOnJava   基于jdk版本为条件

    @ConditionalOnWebApplication 当前项目是web项目的条件下 

    @ConditionalOnNotWebApplication  当前不是web项目的条件下 

    。。。。

    以@ConditionalOnWebApplication 为例,里面依次判断 GenericWebApplicationContext,是否有session的scope,环境是否是servlet环境等来判断是不是web项目。

    4 配置encoding范例

       一般来说,想配置encoding过滤器,要在web.xml里配置以下filter,配置encoding和forceEncoding,使用spring-boot也可以配置filter。

     在 application.properties里面 配置参数 ,然后在配置类HttpEncodingProperties里读取。
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.force=true
    再配置bean
    @Configuration //相当于<beans>
    @EnableConfigurationProperties(HttpEncodingProperties.class//载入配置文件
    @ConditionalOnClass(CharacterEncodingFilter.class)   //CharacterEncodingFilter在类路径下才触发
    @ConditionalOnProperty(prefix = "spring.http.encoding",value = "enabled",matchIfMissing = true) //默认enabled 为true
    public class HttpEncodingAutoConfiguration {

        @Autowired
        private HttpEncodingProperties httpEncodingProperties;

        @Bean //相当于<bean>
        @ConditionalOnMissingBean(CharacterEncodingFilter.class)
        public CharacterEncodingFilter characterEncodingFilter(){
            CharacterEncodingFilter characterEncodingFilter=new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding(httpEncodingProperties.getCharset().name());
            characterEncodingFilter.setForceEncoding(httpEncodingProperties.isForce());
            return characterEncodingFilter;
        }


    }

  • 相关阅读:
    dotnet 控制台读写 Sqlite 提示 no such table 找不到文件
    dotnet 控制台读写 Sqlite 提示 no such table 找不到文件
    dotnet 控制台 Hangfire 后台定时任务
    dotnet 控制台 Hangfire 后台定时任务
    dotnet 获取指定进程的输入命令行
    dotnet 获取指定进程的输入命令行
    PHP sqrt() 函数
    PHP sinh() 函数
    PHP sin() 函数
    PHP round() 函数
  • 原文地址:https://www.cnblogs.com/lkdirk/p/6812371.html
Copyright © 2011-2022 走看看