springboot(3)- 属性注入

    技术2022-07-10  123

    1 banner 配置

    1.1 关闭 banner

    package com.tzb; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class SpringbootTestApplication { public static void main(String[] args) { // SpringApplication.run(SpringbootTestApplication.class, args); // 关闭 bannner SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringbootTestApplication.class); SpringApplication build = builder.build(); build.setBannerMode(Banner.Mode.OFF); build.run(args); } }

    2 tomcat 配置

    2.1 如果不想用 tomcat

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>

    3 spring属性注入

    <dependency> <!-- this is needed or IntelliJ gives junit.jar or junit-platform-launcher:1.3.2 not found errors --> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>

    实体类

    配置文件

    测试类

    @SpringBootTest class SpringbootTestApplicationTests { @Autowired Book book; @Test void contextLoads() { System.out.println("书籍信息: "+book); } }

    3.1 专用配置文件

    4 springboot 类型安全的属性注入

    Processed: 0.011, SQL: 9