一、官方地址

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>

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));
}
}

方式二、使用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));
}
}

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));
}
}

方法二、

@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));
}
}

四、启动时候Spring图标控制

1.关闭

application.setBannerMode(Banner.Mode.OFF);

2.改变启动样式文本

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

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

即可。前提是不关闭。

自定义:也可以在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

004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication、启动图标的更多相关文章

  1. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  2. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  3. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  4. Spring Boot 快速入门 史上最简单

    1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...

  5. Spring Boot 快速入门(IDEA)

    从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...

  6. 基于Spring boot的web项目搭建教程(一)

    前言: 本教程参考了大量前辈的代码,在此不方便一一列举.本教程使用IDEA开发工具搭建项目,对于本人的IDEA已经集成了某些插件,比如Lombok,Thymeleaf,yml等插件,这些插件不在文中提 ...

  7. 笔记61 Spring Boot快速入门(一)

    IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...

  8. spring boot快速入门 1 :创建项目、 三种启动项目方式

    准备工作: (转载)IDEA新建项目时,没有Spring Initializr选项 最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手sprin ...

  9. Spring Boot 快速入门笔记

    Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

随机推荐

  1. (十二)A64

    一.AC108驱动移植 1.驱动添加 cp r18/lichee/linux-4.4/sound/soc/codecs/ac108.* a64/linux-3.10/sound/soc/codecs/ ...

  2. IDEA中使用git合并分支的过程报错:cant checkout because of unmerged files

    使用idea的git插件控制代码分支合并时,由于操作不当,报错了,控制台报错如下: cant checkout because of unmerged files,you have to resolv ...

  3. org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]

    错误: 在 IDEA 创建WEB项目之后,打印出的日志中总是出现一行警告信息: 12-May-2018 15:52:30.692 警告 [RMI TCP Connection(3)-127.0.0.1 ...

  4. JSON 对象和字符串

    JSON 对象和字符串 粘贴自:https://www.cnblogs.com/cstao110/p/3762056.html Q:什么是"JSON字符串",什么是"JS ...

  5. CentOs Linux 对于编辑文本内容时无法退出的几个小命令

    编辑完保存退出的四种方式 1. Esc+:+wq+回车(w是write,q是quit) 2. Esc+:+x+回车(x=wq) 3. Esc+shift+zz 4. Esc+ZZ(在大写开启下)

  6. 一图一知-TS之函数function

  7. jquery属性方法hasClass判断是否存在某个class

    判断匹配集合中是否存在至少一个元素使用样式'selected',存在则返回'true',不存在为'flase'. <html> <head> <script src=&q ...

  8. selenium之chromedriver与谷歌浏览器映射,到谷歌71.0版本的

    转载出处: https://blog.csdn.net/huilan_same/article/details/51896672

  9. RocketMQ原理分析 文章 精选【收集】

    一. 推荐文章 1.以下来自OSChina的 mingxungu https://itzones.cn/ RocketMQ运维监控 RocketMQ刷盘策略 RocketMQ消息重试 RocketMQ ...

  10. NativeRenderingPlugin IOS

    https://bitbucket.org/Unity-Technologies/graphicsdemos/src/77f014c12161e5c25d902e2c5697dd0c45ce3e35/ ...