springBoot

  • 从配置中获取值
    • yml中的值
      • name: ssw
    • java中的代码
      • 第一种配置
        • @Value("${name}")
          String abc;
      • 第二种配置
        • @Autowired
          Environment environment;
          environment.getproperty(“name”);
      • 第三种配置
        • @ConfigurationProperties
          @Component
          public class Person {
          String name;
          }

          @Autowired
          Person person;
  • 校验功能
    • springboot-mvc自带校验功能
      • 例如:
      • @ConfigurationProperties
        @Component
        @Validated

        public class Person {
        @NotNull
        String name;
        }
  • profile用来完成不同环境下,配置动态切换功能的
    • profile配置方式
      • 多profile文件方式:提供多个配置文件,每个代表一种环境
      • yml多文档方式
        • 在yml中使用 ---分割不同配置
    • profile激活方式
      • 配置文件: 再配置文件中配置:spring.profiles.active=dev
      • 虚拟机参数:在VM options 指定: –Dspring.profiles.active=dev
      • 命令行参数:java-jar xxx.jar –spring.profiles.active=dev
  • 内部配置的加载顺序
    • file:/config/:当前项目下的/config目录下
    • file:/ 当前项目的根目录
    • classpath:/config/:classpath的/config目录
    • classpath:/classpath的根目录
  • 整合junit
    • 若测试类不在启动类的包或子包下
      • @RunWith(SpringRunner.class)
        @SpringBootTest(classes = Demospringboot2Application.class)
        class Demospringboot2ApplicationTests {

        @Value("${name}")
        private String name;
        @Test
        void contextLoads() {
        System.out.println(name);
        }
    • 若在子包下
      • @RunWith(SpringRunner.class)
        @SpringBootTest
        class Demospringboot2ApplicationTests {

        @Value("${name}")
        private String name;
        @Test
        void contextLoads() {
        System.out.println(name);
        }
  • 整合mybatis
    • yml中配置
      • spring:
        datasource:
        url: jdbc:mysql:///health?serverTimezone=GMT%2B8
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        mybatis:
        mapper-locations: classpath:*Mapper.xml
    • java中的配置
      • 如果是注解配置,则接口需要加入@Mapper注解识别 ,也可以在启动类上加入注解
      • @MapperScan({com.ssw.demospringboot2.mapper})
      • 如果是xml配置,yml中配置mapper-locations: classpath:*Mapper.xml
  • 整合redis
    • 如果没有配置,也可以使用redis,默认使用的是本机的redis
    • 如果想指定redis,则需要配置
      • spring:
        redis:
        host: 127.0.0.1
        port: 6379
  • redisTemplate
    • 是封装了jedis的工具类,简化jedis的操作
  • 定时任务
    • 引导类需要加入注解@EnableScheduling
    • 添加定时类
      • @Component
        public class TestQuartz {
        @Scheduled(cron="* * * * * ?")
        public void test(){
        System.out.println("11111");
        }
        }
  • @EnableAutoConfiguration
    • 内部使用@import来加载配置类
    • 配置文件位置META-INF/spring.factories,该配置文件定义了大量的配置类,当springboot启动的时候会自动加载这些配置类,初始化bean
    • 并不是所有的bean都会被初始化,再配置类使用condition来加载满足条件的bean
  • 自定义启动类
    • 定义两个模块
      • 第一个模块是配置模块

        • @Configuration
          @EnableConfigurationProperties(RedisConfig.class)
          public class JedisDemo {

          @Bean
          public Jedis jedis(RedisConfig redisConfig){
          return new Jedis(redisConfig.getUrl(),redisConfig.getPort());
          }

          }
        • @Data
          @ConfigurationProperties(prefix = "myredis")
          public class RedisConfig {

          private String url="127.0.0.1";
          private int port=6379;
          }
        • spring.factories配置
        • # Auto Configure
          org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
          com.ssw.config.JedisDemo
      • 第二个调用配置模块
        • redis-starter模块
          • 只要在pom.xml中引入config模块
          • <dependency>
            <groupId>com.ssw</groupId>
            <artifactId>config</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            </dependency>
  • run方法详解
    • 构造器:
      • 判断是否为web环境
      • 读取META-INF中的spring.factories中的配置
      • 找到main方法
    • run方法
      • 准备运行环境
      • 回调listeren相应的方法
      • 初始化spring容器
        • 组件,导入起步依赖,判断相应的条件@Conditionxxxx
      • 回调applicationRunner和CommandLineRunner接口中的方法
      • https://www.jianshu.com/p/4cebe1274226
  • 打包方式
    • 打jar包方式
      • 只需要正常打包即可,然后在控制台执行命令java –jar jar包地址
    • 打war包方式
      • pom
        • <!-- 打包方式 -->
          <packaging>war</packaging>

          <build>
               <!-- 指定war包名称 -->
               <finalName>springboot</finalName>
               <plugins>
                   <plugin>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-maven-plugin</artifactId>
                   </plugin>
               </plugins>
          </build>

      • 启动类改动
        • @SpringBootApplication
          public class SpringbootDeployApplication extends SpringBootServletInitializer {

              public static void main(String[] args) {
                   SpringApplication.run(SpringbootDeployApplication.class, args);
               }


               @Override
               protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
                   return builder.sources(SpringbootDeployApplication.class);
               }
          }

posted on 2020-03-12 22:41  赟麟  阅读(176)  评论(0编辑  收藏  举报

导航