SpringBoot核心【基本配置】,linux运维教程

public class SpringbootHelloApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootHelloApplication.class, args);

}

}

2.定制Banner


通过入口类启动的时候在控制台我们可以看到一个banner图案,处于兴趣原因们想要改下这个也是可以的,具体如下:

2.1 修改banner图标

1.默认的banner如下

在这里插入图片描述

2.在src/main/resources下新建一个banner.txt文件

3.通过访问http://patorjk.com/software/taag 网站生成字符,如"bobo",然后将生成的字符复制到banner.txt中,

在这里插入图片描述

在这里插入图片描述

4.再次启动即可

在这里插入图片描述

2.2 关闭banner

这个banner其实并没有什么作用,一般情况下我们会关点它,我们来看看如何关掉banner。 main方法中修改如下

public static void main(String[] args) {

SpringApplication app = new SpringApplication(SpringbootHelloApplication.class);

// 关闭banner

app.setBannerMode(Banner.Mode.OFF);

app.run(args);

}

在这里插入图片描述

3.SpringBoot的配置文件


SpringBoot使用一个全局的配置文件application.properties或application.yml,位于src/main/resources目录或者类路径/config下,推荐使用properties的方式配置。

在这里插入图片描述

3.1 tomcat端口号修改

tomcat的端口号默认是8080,我们需要将之修改为8082,并将默认访问路径"/" 修改为"/springboot"

server.port=8082

server.servlet.context-path=/springboot

启动后浏览器访问即可:

在这里插入图片描述

3.2 常规属性配置

前面介绍Spring的时候,我们想要注入properties中的值的时候我们需要通过@PropertySource指明properties文件的位置,然后通过@Value注入值,在SpringBoot中,我们只需要在application.properties定义属性,直接使用@Value注入即可。

1.application.properti

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

es增加属性

user.username=波波烤鸭

user.age=18

user.address=深圳

2.代码中获取

/**

  • @program: springboot-hello

  • @description: Hello 案例

  • @author: 波波烤鸭

  • @create: 2019-05-08 21:10

*/

@RestController

public class HelloController {

@Value("${user.username}")

private String name;

@Value("${user.age}")

private int age;

@Value("${user.address}")

private String address;

@RequestMapping("/hello")

public String hello(){

return "Hello SpringBoot … “+name+” “+age+” "+address;

}

}

3.访问测试

在这里插入图片描述

4.中文乱码处理

内容是取到了,但是中文乱码,如何解决呢,继续往下看

在application.properties中添加如下设置

server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

spring.http.encoding.force=true

spring.messages.encoding=UTF-8

file – > setting – > Editor – > File Encodings – >然后按照如下图设置

在这里插入图片描述

再测试

在这里插入图片描述

搞定~

3.3 类型安全的配置

上面将的属性配置在实际的开发过程中有点复杂,因为我们需要设置的属性太多了,这时我看可以使用类型安全的配置方式来实现,这样使用起来会更加方便,具体如下:

1.application.properties中配置

users.name=波波烤鸭

users.age=18

users.address=深圳

2.创建bean对象

@ConfigurationProperties注解需要加入如下依赖:

org.springframework.boot

spring-boot-configuration-processor

true

不然会有错误提示,而且该注解的locations属性在2.x版本中已经移除了!!!

要解决参考此文:https://blog.csdn.net/baidu_19760977/article/details/71206108

/**

  • @program: springboot-hello

  • @description: 用户

  • @author: 波波烤鸭

  • @create: 2019-05-09 17:15

*/

@Component

@ConfigurationProperties(prefix = “users”)

public class User {

private String name;

private String age;

private String address;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值