SpringBoot核心【基本配置】

向右看齐 2022-02-03 13:45 433阅读 0赞

文章目录

  • 基本配置
    • 1.入口类和相关注解
    • 2.定制Banner
      • 2.1 修改banner图标
      • 2.2 关闭banner
    • 3.SpringBoot的配置文件
      • 3.1 tomcat端口号修改
      • 3.2 常规属性配置
      • 3.3 类型安全的配置

  前面两篇文章简单介绍了下SpringBoot及其构建的方式,本文开始介绍下SpringBoot中的一些核心内容。

基本配置

1.入口类和相关注解

  SpringBoot项目通常都有一个名为*Application的入口类,入口类中有一个main方法,这个main方法就是一个标准的java应用的入口方法,方法中使用”SpringApplication.run(…)”,启动SpringBoot应用项目。

在这里插入图片描述
  在入口类中有一个注解”@SpringBootApplication”,这是SpringBoot中的核心主键,它是一个组合组件,进入具体查看如下:

  1. @Target({ ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @SpringBootConfiguration
  6. @EnableAutoConfiguration
  7. @ComponentScan(
  8. excludeFilters = { @Filter(
  9. type = FilterType.CUSTOM,
  10. classes = { TypeExcludeFilter.class}
  11. ), @Filter(
  12. type = FilterType.CUSTOM,
  13. classes = { AutoConfigurationExcludeFilter.class}
  14. )}
  15. )
  16. public @interface SpringBootApplication

  该组合注解中包括这三个:@SpringBootConfiguration ,@EnableAutoConfiguration ,@ComponentScan如果我们不使用@SpringBootApplication注解的话,我们可以分别使用这三个注解来实现。






















注解 作用
@SpringBootConfiguration 继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
@EnableAutoConfiguration 完成依赖的自动配置,比如添加了spring-boot-starter-web依赖,会自动添加tomcat和SpringMVC的依赖,那么SpringBoot会对Tomcat和SpringMVC进行自动配置。
@ComponentScan 会自动扫描@SpringBootApplication所在类的同级包(com.dpb.springboot)以及子包中的Bean,所以建议将入口类放置在groupId+arctifactID组合的包名下

  通过@SpringBootApplication的源码我们会发现SpringBoot会帮助我们完成很多的自动配置,但有些情况下我们并不需要SpringBoot帮助我们完成自动配置,这时我们可以通过@SpringBootApplication注解的exclude参数来设置,如下

  1. @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
  2. public class SpringbootHelloApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringbootHelloApplication.class, args);
  5. }
  6. }

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方法中修改如下

  1. public static void main(String[] args) {
  2. SpringApplication app = new SpringApplication(SpringbootHelloApplication.class);
  3. // 关闭banner
  4. app.setBannerMode(Banner.Mode.OFF);
  5. app.run(args);
  6. }

在这里插入图片描述

3.SpringBoot的配置文件

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

在这里插入图片描述

3.1 tomcat端口号修改

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

  1. server.port=8082
  2. server.servlet.context-path=/springboot

启动后浏览器访问即可:

在这里插入图片描述

3.2 常规属性配置

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

1.application.properties增加属性

  1. user.username=波波烤鸭
  2. user.age=18
  3. user.address=深圳

2.代码中获取

  1. /** * @program: springboot-hello * @description: Hello 案例 * @author: 波波烤鸭 * @create: 2019-05-08 21:10 */
  2. @RestController
  3. public class HelloController {
  4. @Value("${user.username}")
  5. private String name;
  6. @Value("${user.age}")
  7. private int age;
  8. @Value("${user.address}")
  9. private String address;
  10. @RequestMapping("/hello")
  11. public String hello(){
  12. return "Hello SpringBoot ... "+name+" "+age+" "+address;
  13. }
  14. }

3.访问测试

在这里插入图片描述

4.中文乱码处理
内容是取到了,但是中文乱码,如何解决呢,继续往下看
在application.properties中添加如下设置

  1. server.tomcat.uri-encoding=UTF-8
  2. spring.http.encoding.charset=UTF-8
  3. spring.http.encoding.enabled=true
  4. spring.http.encoding.force=true
  5. spring.messages.encoding=UTF-8

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

在这里插入图片描述
再测试

在这里插入图片描述
搞定~

3.3 类型安全的配置

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

  1. users.name=波波烤鸭
  2. users.age=18
  3. users.address=深圳

2.创建bean对象
  @ConfigurationProperties注解需要加入如下依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. <optional>true</optional>
  5. </dependency>

不然会有错误提示,而且该注解的locations属性在2.x版本中已经移除了!!!
要解决参考此文:https://blog.csdn.net/baidu_19760977/article/details/71206108

  1. /** * @program: springboot-hello * @description: 用户 * @author: 波波烤鸭 * @create: 2019-05-09 17:15 */
  2. @Component
  3. @ConfigurationProperties(prefix = "users")
  4. public class User {
  5. private String name;
  6. private String age;
  7. private String address;
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public String getAge() {
  15. return age;
  16. }
  17. public void setAge(String age) {
  18. this.age = age;
  19. }
  20. public String getAddress() {
  21. return address;
  22. }
  23. public void setAddress(String address) {
  24. this.address = address;
  25. }
  26. }

3.业务代码

  1. /** * @program: springboot-hello * @description: Hello 案例 * @author: 波波烤鸭 * @create: 2019-05-08 21:10 */
  2. @RestController
  3. public class HelloController {
  4. @Autowired
  5. private User user;
  6. @RequestMapping("/hello")
  7. public String hello(){
  8. return "Hello SpringBoot ... "+user.getName()+" "+user.getAge()+" "+user.getAddress();
  9. }
  10. }

4.测试

在这里插入图片描述
搞定~

发表评论

表情:
评论列表 (有 0 条评论,433人围观)

还没有评论,来说两句吧...

相关阅读