2-SpringBoot知识点-Banner、三种容器(tomcat、jetty、undertow)、property、ymal、profile

    技术2026-04-06  5

    一、Banner

    介绍:springboot运行时欢迎的图标

    自定义欢迎图标 在Resource下添加banner.txt,在文本内添加内容即可 运行结果: 定制网站

    https://patorjk.com/software/taag

    关闭Banner @SpringBootApplication public class BannerApplication { public static void main(String[] args) { SpringApplicationBuilder builder = new SpringApplicationBuilder(BannerApplication.class); SpringApplication build = builder.build(); //关闭banner build.setBannerMode(Banner.Mode.OFF); build.run(args); }

    运行结果:

    二、容器

    tomcat pom.xml //web启动器中自带了tomcat <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

    application.properties # 设置端口号 server.port=8081 # 设置上下文路径 server.servlet.context-path=/lyl # 设置tomcat的URL的编码 server.tomcat.uri-encoding=UTF-8 编写控制器进行访问 jetty pom.xml 注:要使用jetty容器,必须先去除内置的tomcat。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--去除内置tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--使用jetty容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> undertow容器 注:与jetty一样 <!--使用undertow容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>

    三、property

    类型安全的属性注入 @Component //加载resource下自己建立的配置文件。ymal文件暂时不支持 @PropertySource("classpath:book.properties") //类型安全的属性注入:该注解会自动的去查找配置文件中前缀为book的key,并将值赋给实体类中的属性。(springboot提供的注解) @ConfigurationProperties(prefix = "book") public class BookSb { private Long id; private String name; private String author; } spring的属性注入 //注:@Component、@PropertySource、@Value这三个都是spring中的注解 @Component //加载resource下自己建立的配置文件 @PropertySource("classpath:book.properties") public class Book { @Value("${book.id}") private Long id; @Value("${book.name}") private String name; @Value("${book.author}") private String author; }

    四、ymal

    ymal配置文件与properties配置文件的区别 ymal配置是有序的,properties配置是无序的自定义的ymal配置暂时不支持使用注解的方式注入到springboot项目中 ymal配置对数组的友好支持 @Component @ConfigurationProperties(prefix = "redis") public class RedisCluster { private Integer port; private List<String> hosts; private List<Redis> redisList; } //Redis public class Redis { private Integer port; private String host; } //application.ymal redis: port: 6379 hosts: - 192.168.1.30 - 192.168.1.31 - 192.168.1.32 redisList: - port: 6379 host: 192.168.1.34 - port: 6380 host: 192.168.1.65

    六、profile

    介绍:不同的环境对应不同的配置文件

    切换配置文件 spring.profiles.active=prod
    Processed: 0.010, SQL: 9