• 004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication、启动图标


    一、官方地址

    Spring:http://spring.io/

    Spring Project:http://spring.io/projects

    Spring boot:https://projects.spring.io/spring-boot/

    帮助文档:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/

    二、项目搭建

    方式一、项目搭建继承父pom方式【不推荐】

    1)新建maven项目→使用默认配置即可

    定义好项目名称等

    这里packaging必须选择jar而不是war,spring boot项目最终会打成一个jar包而不是war包

    2)修改jdk版本

        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    View Code

    3)增加父pom引入

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
        </parent>

    在pom的dependencies增加依赖

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

    4)增加启动类

    @SpringBootApplication
    public class App {
        @Bean
        public Runnable createRunnable() {
            return () -> {
                System.out.println("spring boot is running");
            };
        }
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
            context.getBean(Runnable.class).run();
            System.out.println(context.getBean(User.class));
        }
    }
    View Code

    方式二、使用spring-boot-dependencies【推荐】

    第一步、第二步、第四步同方式一

    3)增加spring-boot-dependencies

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

    在pom的dependencies增加依赖

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

    注意:一般dependency不需要配置版本因为在dependencyManagement已经默认了版本

    三、浅析

    1、SpringBootApplication

    @SpringBootApplication是spring boot最重要的一个注解,用于快捷配置启动类

    默认扫描包的路径是当前包下面的所有路径,可以通过修改scanBasePackages修改扫描路径

    exclude:排除

    excludeName:根据类名排除

    2、三个关键的注解

    在注解@SpringBootApplication,上有三个关键的注解

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(excludeFilters = {
            @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
            @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

    @ComponentScan:用注解配置实现自动扫描,默认会扫描当前包和所有子包,和xml配置自动扫描效果一样,@Filter是排除了两个系统类

    @EnableAutoConfiguration:实现自动配置

    @SpringBootConfiguration:同spring中的@Configuration几乎一致,标记当前类是一个配置类,就像xml配置文件,而现在是用java配置文件,效果是一样的

    @Bean:就是在spring配置文件中声明了一个bean,同xml一致。

    一般项目结构,共三个文件,启动类、配置类、业务类

     注意:如果示例比较简单可以直接使用ComponentScan注解即可。没有用到enable特性

    @ComponentScan
    public class App2 {
        @Bean
        public Runnable createRunnable() {
            return () -> {
                System.out.println("spring boot is running");
            };
        }
    
        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));
        }
    }
    View Code

    3、启动

    方法一、SpringApplication.run(App2.class, args);

    这里默认会将第一个参数的类默认变为配置类@Configuration

    @ComponentScan
    public class App2 {
        @Bean
        public Runnable createRunnable() {
            return () -> {
                System.out.println("spring boot is running");
            };
        }
    
        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));
        }
    }
    View Code

    方法二、

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

     四、启动时候Spring图标控制

    1.关闭

    application.setBannerMode(Banner.Mode.OFF);

    2.改变启动样式文本

    默认:在classpath下增加banner.txt文件

    短歌行
    作者:曹操
    对酒当歌,人生几何!
    譬如朝露,去日苦多。
    慨当以慷,忧思难忘。
    何以解忧?唯有杜康。
    青青子衿,悠悠我心。
    但为君故,沉吟至今。
    呦呦鹿鸣,食野之苹。
    我有嘉宾,鼓瑟吹笙。
    明明如月,何时可掇?
    忧从中来,不可断绝。
    越陌度阡,枉用相存。
    契阔谈�,心念旧恩。
    月明星稀,乌鹊南飞。
    绕树三匝,何枝可依?
    山不厌高,海不厌深。
    周公吐哺,天下归心。
    View Code

    即可。前提是不关闭。

    自定义:也可以在application.properties中修改文件路径以及编码,默认是utf-8

    banner.location=mybanner.txt
    banner.charset=GBK

    3.改变启动样式文件格式图片

    springboot支持图片的banner,将图片放置在classpath,图片格式jpg、png、gif

    默认:名称是banner.jpg或其他格式

    自定义:也可以在application.properties中修改文件路径

    banner.image.location=my.jpg
  • 相关阅读:
    python如何进行内存管理的
    多进程于多线程的区别,cpu密集型适合用什么
    __new__() 与__init__()的区别
    orm的理解
    三次握手四次挥手
    HTTP、TCP、IP、协议
    mysql字段添加修改删除
    django本身提供了runserver,为什么不能用来部署(runserver与uWSGI的区别)
    django中scrf的实现机制
    django重定向是如何实现的,用的什么状态码?
  • 原文地址:https://www.cnblogs.com/bjlhx/p/8324971.html
Copyright © 2020-2023  润新知