springboot学习笔记|第一篇:基础配置

1.前言

该springboot学习系列文章主要参考《Spring Boot+Vue全栈开发实战》这本书,该系列文章可以说是该本书的学习笔记吧,参考的书籍讲解非常详细,作者对于每一个细节都有截图,通过此书学习让我对于springboot有了更深入的认识,想学习springboot的同鞋可以好好学习一下这本书

2.@SpringBootApplication

@SpringBootApplication分别由以下三个注解组成,该注解也可用以下三个注解组合代替

注解作用
@SpringBootConfiguration表示这是一个配置类
@EnableAutoConfiguration开启自动化配置
@ComponentScan完成包扫描,默认扫描当前包及其子包
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM,
				classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}

3.springboot banner

  • 定制banner

    • 访问艺术字网站生成banner,http://www.network-science.de/ascii/
    • 将生成的艺术字拷贝至banner.txt,放至项目resource文件夹中
    • 启动服务,即可查看到修改后banner
  • 关闭banner

    通过Mode.OFF关闭banner

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
    builder.bannerMode(Mode.OFF).run(args);
  }
}

4.Tomcat配置

  • 常规配置

    # 服务端口
    server.port=8080
    # 错误跳转页面 
    server.error.path=/error
    # session过期时间
    server.servlet.session.timeout=30m
    # 请求服务前缀
    server.servlet.context-path=/hello
    # tomcat编码
    server.tomcat.uri-encoding=utf-8
    # tomcat最大线程
    server.tomcat.max-threads=500
    # tomcat运行日志和临时文件目录
    server.tomcat.basedir=/home/milk/tmp
    
  • https配置

    • 使用jdk自带的keytool生成数字证书
    $ keytool -genkey -alias tomcathttps -keyalg RSA -keysize 2048 -keystore /home/milk/Desktop/https.p12 -validity 365
    

    -genkey:表示要创建一个新的密钥

    -alias:表示别名

    -keyalg:表示加密算法

    -keysize:表示密钥长度

    -keystroe:表示生成密钥存放位置

    -validity:表示密钥有效时间,单位为天

    • application.properties配置
    server.ssl.key-store=/home/milk/Desktop/https.p12
    server.ssl.key-alias=tomcathttps
    server.ssl.key-store-password=qq123456
    
    • 访问,只能通过https进行访问

5.properties配置

  • 配置文件访问优先级如下

[外链图片转存失败(img-iSiUNQRN-1566810017680)(/home/zycao/Desktop/QQ图片20190826162342.png)]

  • 通过命令行,指定自定义配置

    # spring.config.name:指定配置文件名字,spring.config.location:指定路径
    $ java -jar hello-SNAPSHOT.jar --spring.config.name=app --spring.config.location=classpath:/
    

6.自定义配置

  • @Value使用配置属性

    • 配置
    server.port=8080
    student.name=zhangsan
    student.id=201245
    
    • 使用
    @RestController
    public class HelloController {
      @Value("${student.name}")
      private String name;
      @Value("${student.id}")
      private String num;
    
      @GetMapping("/index")
      public String index(){
        System.out.println(name);
        System.out.println(num);
        return "hello world";
      }
    }
    
  • @ConfigurationProperties(prefix="")使用配置属性

    • 配置
    @Component
    @ConfigurationProperties(prefix = "student")
    @Data
    public class Student {
      private String name;
      private String id;
    }
    
    • 使用
    @RestController
    public class HelloController {
      @Resource
      private Student student;
      @GetMapping("/index")
      public String index(){
        System.out.println(student);
        return "hello world";
      }
    }
    
  • yml使用集合

    • 配置
    student:
      name: zhangsan
      address: cq
      hobby:
        - foot
        - basketball
    

7.profile

根据不同环境指定不能配置文件

  • 创建配置文件application-dev.properties
  • 配置文件中配置使其生效
spring.profiles.active=dev
  • 代码中配置生效
@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
    builder.bannerMode(Mode.OFF).application().setAdditionalProfiles("prod");
    builder.run(args);
  }
}

8.参考资料

  • 《Spring Boot+Vue全栈开发实战》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值