springboot(3)- 屬性注入

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 類型安全的屬性注入

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章