我们为什么需要 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题目,扒各种算法套路的裤子”
?从Ngin到Pandownload,程序员如何避免面向监狱编程?
?从Web1.0到Web3.0:详析这些年互联网的发展及未来方向

关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 《求是》发表习近平总书记重要文章 7904819
- 2 销售直言不建议买35万玛莎拉蒂 7808939
- 3 警方通报老人坐门口吃橘子被打倒 7712539
- 4 明年经济工作政策取向确立这八个字 7616016
- 5 《怦然心动》导演家中死亡 其子已被捕 7519802
- 6 9天涨粉400万的“蛋神”发声 7424337
- 7 泰柬冲突急转直下 世界三个没想到 7331645
- 8 金建希被曝曾深夜激烈怒斥尹锡悦 7235087
- 9 香奈儿“购物篮”包拍出超百万天价 7143307
- 10 如何让你我的钱袋子鼓起来 7045858







CSDN
