第一节:SpringBoot基础配置

SpringBoot项目启动方式

# 开发工具内启动
直接运行项目启动类的main方法

# maven命令启动
mvn spring-boot:run

# 打jar包启动
mvn clean package
java -jar demo.jar

# 打war包发布到tomcat启动

spring-boot-starter-parent

<!-- 提供项目基础依赖包及版本管理 -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/>
</parent>

SpringBoot启动类注解:@SpringBootApplication

// 启动类
@SpringBootApplication
public class Demo1Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}


@SpringBootApplication ,此注解为组合注解,包含如下
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan


@ComponentScan 可以扫描与其类同级和子级的被以下注解修饰的类
    @Service
    @Repository
    @Component
    @Controller
    @RestController
    @Configura

SpringBoot自动化配置原理

# 实现自动化配置的注解
@EnableAutoConfiguration

# 关闭自动配置
@EnableAutoConfiguration(exclude = {KafkaAutoConfiguration.class})

修改项目启动banner

# 在resources/目录下创建banner.txt

# banner艺术字参考网址
http://www.network-science.de/ascii/

Web容器配置

application.propertices文件内配置

server.port=8081		        //端口号
server.error.path=/error		//当项目出错时跳转去的页面
server.servlet.session.timeout=30m	// session失效时间 30m表示30分,如果不写单位,默认单位秒
server.servlet.context-path=/aiexam	//项目名称,不配置默认/。配置则在访问路径中加上配置的路径 http://localhost:8081/aiexam/
server.tomcat.uri-encoding=utf-8	//tomcat请求编码
server.tomcat.max-threads=500		//tomcat最大线程数
server.tomcat.basedir=/home/sang/tmp	//存放tomcat运行日志和临时文件的目录,若不配置,默认使用系统的临时目录

Properties配置: application.properties 或 application.yml

application.properties配置文件一共可以出现在如下4各位置

项目根目录下的config文件夹中	/config/application.properties	读取优先级1最高
项目根目录下	                /application.properties	读取优先级2
classpath下的config文件夹中       /src/main/resources/config/application.properties 读取优先级3
classpath下	                /src/main/resource/application.properties 读取优先级4
					
					
					
分环境配置application.properties
	application-dev.properties		开发环境配置
	application-stg.properties		测试环境配置
	application-prd.properties		生产环境配置
					
在application.propertices文件内配置使用的环境配置文件
	spring.profiles.active=dev #表示使用application-dev.properties配置文件启动项目

在application.properties中定义全局变量及读取

在application.properties中定义全局变量
    book.name=三国演义
    book.author=罗贯中
    book.price=30
					

// 获取方式
@Component
@ConfigurationProperties(prefix = "book")
public class Book {
    private String name;
    private String author;
    private float price;
    getter/setter...
}

或直接读取
@Component
public class Book {
    //通过${}获取application.properties里的自定义属性
    @Value("${book.name}")
    private String name;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值