我们为什么需要 SpringBoot?
作者 | 阿文,责编 | 郭芮
头图 | CSDN 下载自东方IC
出品 | CSDN(ID:CSDNnews)
小试牛刀
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class);
}
}
@EnableAutoConfiguration
@ComponentScan
我们可以在项目下新建一个 HelloController:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello World!";
}
}
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
mvn package
java -jar target/demo-0.0.1-SNAPSHOT.jar
定制Banner
SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class);
builder.bannerMode(Banner.Mode.OFF).run(args);
Web 容器的配置
server.address=127.0.0.1 # 配置地址
server.port=8888 # 配置端口
server.tomcat.basedir=/opt/tmp # 配置目录
server.tomcat.uri-encoding=utf-8 #配置编码
server.tomcat.max-threads=300 #配置最大线程数
server.ssl.key-store= #配置秘钥文件名称
server.ssl.key-alias= #配置秘钥别名
server.ssl.key-password= # 配置证书密码
application.properties 的文件加载顺序
项目根目录下的config 文件夹中
项目的根目录下
classpath 下的config文件夹下
classpath 下
jar -jar xxx.jar --spring.config.name=xxx
jar -jar xxx.jar --spring.config.location=classpath:/
book.name=西游记
book.author=六承恩
book.price=66
book.type="古典文学","四大名著"
@Component
@ConfigurationProperties(prefix = "book")
public class Book {
private String name;
private String author;
private Float price;
private List<String> type;
//getter 省略
//seteer 省略
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type=" + type +
'}';
}
}
@RestController
public class BookController {
@Autowired
Book book;
@GetMapping("/book")
public String book(){
return book.toString();
}
}
book:
name: 西游记
author: 六承恩
price: 66
type:
- 古典文学
- 四大名著
YAML格式的文件虽然方便,但是无法使用@PropertySource 注解加载YAML文件。
Profile
SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class);
builder.application().setAdditionalProfiles("prod");
builder.run(args);
--spring.profiles.active=prod
。
【END】
更多精彩推荐
☞“手把手撕LeetCode题目,扒各种算法套路的裤子”
☞
☞