springboot 初探 、基础及配置

一、spring boot 的核心功能
  独立运行的spring项目、内嵌servlet容器、提供starter简化maven配置、自动配置Spring、准生产的应用监控、无代码生成和xml配置
二、spring boot的优点:
  快速构建项目;对主流开发框架的无配置集成 ;项目可独立运行,无须外部依赖servlet容器;
  提供运行时的应用监控;极大地提高了开发、部署效率;与云计算的天然集成
三、spring boot的基本配置
  1、入口类和@SpringBootApplication
    @SpringBootApplication组合了@Configuration@EnableAutoConfiguation和@ComponetScan;

    其中@EnableAutoConfiguation让Springboot根据类路径中的jar包依赖为当前项目进行自动配置
  2、关闭特定的自动配置
    使用@SpringBootApplication的注解exclude参数
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.calss})
  3、定制banner
  4、springboot的配置文件
    application.properties或者application.yml,放置在src/main/resources目录或者类路径的/config下
  5、starter pom 包括官方提供的starter pom和第三方提供的starter pom
  6、使用xml配置
    使用spring提供的@ImportResource来加载xml配置
    @ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})
四、spring boot的外部配置
  spring boot 允许使用properties文件、yaml文件或者命令行参数作为外部配置
  1、命令行参数配置 java -jar xx.jar --server.port=9999
  2、常规属性配置 直接使用@value注入相对应的application.properties中的属性即可
  3、基于类型安全的配置
  通过@ConfigurationProperties讲propertis属性和一个Bean及其属性关联
    如:application.properties中配置
      author.name=wyf
      author.age=32
    可以建立一个类型安全的Bean 如下
    @Component
    @ConfigurationProperties(prefix="author")
    public class AuthorSettings{
    private String name;
    private Long age;
    -----
  }
  @ConfigurationProperties加载属性文件内的配置通过prefix属性指定属性的配置前缀,通过locations指定属性文件的位置
五、日志配置
  spring boot 默认使用logback作为日志框架,已为当前使用日志框架的控制台输出及文件输出做好了配置
  logging.file=D:/mylog/log.log
  logging.level.包名=级别
六、Profile配置
  profile 是spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties 如application-prod.properties

  通过在appliction.properties中设置spring.profiles.active=prod 来制定活动的Profile

猜你喜欢

转载自www.cnblogs.com/GtShare/p/9268594.html