SpringBoot随笔1

男娘i 2022-12-23 09:44 223阅读 0赞

@SpringBootApplication注解

  1. package com.example.myapplication;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

@SpringBootApplication注解用来标识你的主类,如果不想使用该注解,官方提出可以使用@EnableAutoConfiguration 和 @ComponentScan和@Configuration组合来代替,也可以实现相同的功能!

@EnableAutoConfiguration:开启自动配置
@ComponentScan:扫描组件
@Configuration: 允许在上下文中注册额外的 bean 或者导入额外的配置类

If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines that behaviour so you can also use those instead.

2、Spring官方建议的包结构如下:
在这里插入图片描述
3、导入额外的配置类

You need not put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

意思就是我们没有必要将所有的自定义的配置类都放在一个类里面,可以搞好几个类,然后使用@Import注解导入,也可以使用@ComponentScan注解进行批量导入。

4、导入 XML 配置

If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an @ImportResource annotation to load XML configuration files.

如果我们需要用到XML配置文件进行Bean的声明和配置,我们就可以使用@ImportResource注解来把XMl的Bean导入到SpringBoot容器中。

5、自动配置
如果想知道SpringBoot在启动的时候都自动配置了哪些应用,可以开启debug调试进行查看

f you need to find out what auto-configuration is currently being applied, and why, start your application with the —debug switch. Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.

可以使用java -jar app —debug
也可以在配置文件中加入debug=true即可

在这里插入图片描述
6、禁用特定的自动配置类
由于SpringBoot启动的时候会自动配置很多类,如果有你不想使用的类,可以选择排除不加载

  1. import org.springframework.boot.autoconfigure.*;
  2. import org.springframework.boot.autoconfigure.jdbc.*;
  3. @SpringBootApplication(exclude={ DataSourceAutoConfiguration.class})
  4. public class MyApplication {
  5. }

如果这个类不在当前类路路径下,你还可以使用excludeName 来指定其类的完全限定名

7、自动重启

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

加入devtools依赖,在类路径下的代码发生改变以后,点击IDEA的build–build project可以在不重启项目的情况下更改的代码自动生效

8、惰性加载
spring.main.lazy-initialization=true 控制所有的bean延迟加载

如果想为特定Bean指定延迟加载 可以使用@Lazy注解
9.自定义Springboot启动图标
在类路径下放banner.txt/banner.png/gif 等文件,再次重启即可生效

10.关闭Banner

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

发表评论

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

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

相关阅读

    相关 人生随笔-1

    身边有太多人,沉迷于短期快感之中,将生活过得浑浑噩噩,陷入了恶性循环。 比如我:身为一个28岁的程序员,平时的业余的时间,都用在打某者上,上了王者不满足还要上荣耀上了荣耀还要

    相关 SpringBoot随笔

    1、SpringBoot三板斧:加依赖、写注解、写配置 2、运行项目:一般会先mvn clean install +再找到main方法右击运行或 cd target+java

    相关 Android学习随笔(1)

    学习流程来自《第一行代码》(第二版) 最近开始了Android的学习,看到很多人都推荐这一本书,就决定按照这一本书的讲解流程熟悉一下Android。 环境配置 ![