美文网首页springbootkankan(good)
SpringBoot的三种启动方式

SpringBoot的三种启动方式

作者: AC编程 | 来源:发表于2022-11-29 09:31 被阅读0次

    一、Spring Boot的核心功能

    1、 可独立运行的Spring项目:Spring Boot可以以jar包的形式独立运行。
    2、 内嵌的Servlet容器:Spring Boot可以选择内嵌Tomcat、Jetty或者Undertow,无须以war包形式部署项目。
    3、 简化的Maven配置:Spring提供推荐的基础 POM 文件来简化Maven 配置。
    4、 自动配置Spring:Spring Boot会根据项目依赖来自动配置Spring 框架,极大地减少项目要使用的配置。
    5、 提供生产就绪型功能:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。
    6、 无代码生成和xml配置:Spring Boot不生成代码。完全不需要任何xml配置即可实现Spring的所有配置。

    二、SpringBoot的三种启动方式

    2.1 @EnableAutoConfiguration

    @EnableAutoConfiguration的作用是开启自动装配,帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot,并创建对应配置类的Bean,并把该Bean实体交给IoC容器进行管理。

    1、导入相关的jar包

    <?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 http://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.0.8.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.spring</groupId>
        <artifactId>springBoot-demo1</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <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>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    2、新建文件IndexController.java

    package com.spring;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @EnableAutoConfiguration
    @RestController
    public class IndexController {
    
    
         // 访问路径   http://localhost:8080/index
        @RequestMapping("/index")
        public String index(){
            System.out.println("我进来了");
            return "index controller";
        }
    
        public static void main(String[] args) {
            // 启动springboot
            SpringApplication.run(IndexController.class,args);
        }
    }
    

    3、运行main方法,后即可启动项目了

    2.2 @ComponentScan

    @ComponentScan()注解的作用是根据定义的扫描路径,将符合规则的类加载到spring容器中,比如在类中加入了以下注解 @Controller、@Service、@Mapper 、@Component、@Configuration 等等。

    1、创建启动类App.java

    package com.spring.controller;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    
    @EnableAutoConfiguration  // 开启自动装配
    @ComponentScan("com.spring.controller")
    public class App {
    
        public static void main(String[] args) {
            // 启动springboot
            SpringApplication.run(App.class,args);
        }
    }
    

    2、控制层 OrderController.java

    package com.spring.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class OrderController {
    
        @RequestMapping("/order")
        public String order(){
            System.out.println("我进来了 order controller 的 index 方法");
            return "order controller";
        }
    }
    
    2.3 @SpringBootApplication

    @SpringBootApplication注解的作用是标注这是一个springboot的应用,被标注的类是一个主程序, SpringApplication.run(App.class, args);传入的类App.class必须是被@SpringBootApplication标注的类。@SpringBootApplication是一个组合注解,组合了其他相关的注解,点进去注解后我们可以看到,这个注解集成了以上2种启动方式的注解;在这里的@ComponentScan()注解有一堆东西,它的作用是 将主配置类所在包及其下面所有后代包的所有注解扫描。

    SpringBootApplication源码:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    public @interface SpringBootApplication {
        @AliasFor(
            annotation = EnableAutoConfiguration.class
        )
        Class<?>[] exclude() default {};
    
        //其他代码省略
    }
    

    SpringBootApplication使用起来更加简单,只需要一个注解即可完成

    package com.spring.controller;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            // 启动springboot
            SpringApplication.run(App.class,args);
        }
    }
    

    相关文章

      网友评论

        本文标题:SpringBoot的三种启动方式

        本文链接:https://www.haomeiwen.com/subject/hekkfdtx.html