springboot原理實戰(4)-springboot入口分析

目錄

從現在開始進入springboot項目入門,先來張本文的概要腦圖:
在這裏插入圖片描述

一、環境搭建2種方式

這裏說的是Pom引入springboot的包的方式:

①繼承父組件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>springboot2-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot2-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

關鍵是這裏:
在這裏插入圖片描述

②第2種引入方式:

不繼承spring-boot-starter-parent。
我們從maven中央倉庫找到這個包:
在這裏插入圖片描述
修改下:
這樣引入:

 <dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <scope>import</scope>
    <type>pom</type>
</dependency>

全部的pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.demo</groupId>
    <artifactId>springboot2-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot2-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.2.5.RELEASE</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

上面的2種方式都可以。

二.@SpringBootApplication註解分析

我們看下啓動類上有個註解@SpringBootApplication註解
,這個註解,滿足我們的所有想象,幫我們做了都很多的工作。
來段代碼:

@SpringBootConfiguration
public class Springboot2DemoApplication {

    @Bean
    public Runnable createRunnable(){
        return () -> {
            System.out.println("spirng boot is started");
        };
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Springboot2DemoApplication.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
    }

}

點擊註解看到,它是個組合註解:
在這裏插入圖片描述

  • @ComponentScan 可以掃描當前包下的組件。
  • @SpringBootConfiguration把入口類配置成一個Configuration配置類。它包含了
    @Configuration註解。
    在這裏插入圖片描述
  • @EnableAutoConfiguration自動配置,功能強大,以後說。

案例1:入口替換爲@ComponentScan

定義一個bean

@Component
public class User {
}

修改入口註解:

@ComponentScan
public class App2 {
    @Bean
    public Runnable createRunnable(){
        return () -> {
            System.out.println("spirng boot is started");
        };
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
        System.out.println(context.getBean(List.class));
    }

}

運行結果:
在這裏插入圖片描述
完美。
說明什麼?

說明@ComponentScan掃描到了User對象,容器拿到這個bean。
但是這個入口類爲啥能拿到Runnable這個類?我們明明去掉了SpringBootConfiguration,不能把這個類當成配置類了啊。

解答:
我們看下入口的run的方法:
在這裏插入圖片描述
在這裏插入圖片描述
答案在這裏。
這裏會把入口類這個resouce類默認配置到容器中,所以可以當做配置類來使用。

案例2:@SpringBootConfiguration替換@Configuration

演示@SpringBootConfiguration是否有配置功能:

@SpringBootConfiguration
public class MyConfig {

    @Bean
    public List createList(){
        return new ArrayList();
    }
}

測試:

@ComponentScan
public class Springboot2DemoApplication {

    @Bean
    public Runnable createRunnable(){
        return () -> {
            System.out.println("spirng boot is started");
        };
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Springboot2DemoApplication.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(List.class));
    }

}

在這裏插入圖片描述
可以看出已經注入進來了。

三、兩種啓動方式

①默認的靜態方法運行

在這裏插入圖片描述

官方默認是這種方式,使用SpringApplication調用run方法,運行整個springboot項目。現在換一種方式啓動:

②new SpringApplication()方式啓動

@ComponentScan
public class App {
@Bean
    public Runnable createRunnable(){
        return () -> {
            System.out.println("spirng boot is started");
        };
    }
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(App.class);
//        Set<String> sets = new HashSet<>();
//        sets.add(App.class.getName());
//        app.setSources(sets);
        ConfigurableApplicationContext context = app.run(args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
    }
}

看下效果:
在這裏插入圖片描述
也可以這樣寫:

@ComponentScan
public class App {
   @Bean
    public Runnable createRunnable(){
        return () -> {
            System.out.println("spirng boot is started");
        };
    }
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication();
        Set<String> sets = new HashSet<>();
        sets.add(App.class.getName());
        app.setSources(sets);
        ConfigurableApplicationContext context = app.run(args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
    }
}

結果完美:
在這裏插入圖片描述
同時,還可以調用其他的入口:
比如從新寫個類,沒有@ComponentScan,當前類也沒有注入bean

public class App2 {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication();
        Set<String> sets = new HashSet<>();
        sets.add(App.class.getName());
        app.setSources(sets);
        ConfigurableApplicationContext context = app.run(args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
        System.out.println(context.getBean(List.class));
    }

}

可以看到這裏調用的是其他的入口類
在這裏插入圖片描述
顯示結果也ok:

在這裏插入圖片描述


個人微信公號:
搜索: 怒放de每一天
不定時推送相關文章,期待和大家一起成長!!
在這裏插入圖片描述


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