SpringBoot 02: 初识SpringBoot

1. SpringBoot产生原因

  • spring, springmvc框架使用上的一些缺点:
  • 【SpringBoot 02: 初识SpringBoot】需要使用的大量的配置文件
  • 还需要配置各种对象
  • 需要把使用的对象放入到spring容器中才能使用对象
  • 需要了解其他框架配置规则
  • springboot的一些直观优点:
  • SpringBoot就相当于简化了配置文件的Spring+SpringMVC(但springboot的核心还是IOC容器)
  • 常用的框架和第三方库都已经配置好了, 只需要引入使用即可
特点
  • Create stand-alone Spring applications
可以创建spring应用
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
内嵌的tomcat, jetty, Undertow
  • Provide opinionated 'starter' dependencies to simplify your build configuration
提供了starter起步依赖, 简化应用的配置:比如使用MyBatis框架, 需要在Spring项目中, 需要配置MyBatis的对象, SqlSessionFactory以及Dao的代理对象但在SpringBoot项目中, 只要在pom.xml里面加入一个mybatis-spring-boot-starter依赖
  • Automatically configure Spring and 3rd party libraries whenever possible
尽可能去自动配置spring和第三方库, 叫做自动配置(就是把spring中的,第三方库中的对象都创建好,放到容器中,开发人员可以直接使用)
  • Provide production-ready features such as metrics, health checks, and externalized configuration
提供了健康检查, 统计, 外部化配置
  • Absolutely no code generation and no requirement for XML configuration
不用生成代码,不需要使用xml文件做配置2. SpringBoot项目地址
  • 创建springboot项目时,可能用到的地址
  • 国外地址: https://start.spring.io
  • 国内地址: https://start.springboot.io
@SpringBootApplication注解
  • 位于项目启动类上面,是复合注解, 包含以下注解
@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
  • 而@SpringBootConfiguration又是包含@Configuration的符合注解
@Configurationpublic @interface SpringBootConfiguration {@AliasFor(annotation = Configuration.class)boolean proxyBeanMethods() default true;}//说明使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,可以使用Bean声明对象,注入到容器
  • @EnableAutoConfiguration
启用自动配置,把java对象配置好,注入到spring容器中 。例如可以把mybatis的对象创建好,放入到容器中
  • @ComponentScan
扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等 。默认扫描的包:@ComponentScan所在的类,以及其所在类所在的包和子包 。SpringBoot的配置文件
  • 名称:application
  • 后缀:property(key=value) 或 yml(key:value)
  • 配置文件示例:
  • 例1:application.properties设置端口和上下文
#设置端口号server.port=9090#设置访问应用上下文路径,contextpathserver.servlet.context-path=/myboot
  • 例2:application.yml,配置文件的结构更加清晰,推荐使用
server:port: 9090servlet:context-path:/myboot多环境配置文件