目录
一、SpringBoot的定义
二、Spring Boot 优点
三、创建一个springboot的项目
四、使用IDEA创建SpringBoot失败案例
一、SpringBoot的定义
Spring 的诞⽣是为了简化 Java 程序的开发的,⽽ Spring Boot 的诞⽣是为了简化 Spring 程序开发的。 Spring Boot 翻译⼀下就是 Spring 脚⼿架,就是为了快速开发 Spring 框架⽽诞⽣的Spring Boot 项⽬的⼀大特点:约定⼤于配置。较于Spring需要配置Bean的扫描路径,而SpringBoot就不需要
二、Spring Boot 优点
- 快速集成框架,Spring Boot 提供了启动添加依赖的功能,⽤于秒级集成各种框架。
- 内置运⾏容器,⽆需配置 Tomcat 等 Web 容器,直接运⾏和部署程序。
- 快速部署项⽬,⽆需外部容器即可启动并运⾏项⽬。
- 可以完全抛弃繁琐的 XML,使⽤注解和配置的⽅式进⾏开发。
- ⽀持更多的监控的指标,可以更好的了解项⽬的运⾏情况。
SpringBoot的核心: 1、快速添加依赖 2、内置web容器 3、自动装配(即SpringBoot自动构建复杂对象,构建好的对象可以直接使用)
三、创建一个springboot的项目
因为我们⽤的 Idea 社区版2021.3,选择安装SpringBoot插件才能创建 Spring Boot 项目。 SpringBoot插件名字为
接下来开始正式创建SpringBoot项目:
1、先选择file->new->project
2、选择Spring Initializr,选择国外的默认配置源
3、添加项目的依赖 一般是Web项目,所以我们勾选最基本的 Web选项,选择SpringBoot Devtools用于实现热部署,其他可以等用到的时候再添加。
Spring BootDevtools:实现热部署
Lombok:Java注解
Spring Web: 外部服务
SpringBoot版本选择后缀不带英文字母的稳定版本,其中低版本的比较稳定
点击下一步,命名自己项目的名字和保存地址,点击finash,等待项目的创建 开始进行启动类初始化:右击 项目名,选择“ Add Framework Support”
如果出现以下蓝色且右上角带有三角形则启动类初始化成功
点击运行启动类,出现以下图形则说明一个SpringBoot项目已经构建完成
四、使用IDEA创建SpringBoot失败案例
由于SpringBoot版本可能与IDEA某些版本产生冲突导致无法创建成功,失败的案例有如下几种:
1、Maven缺少plugins和dependencies两个选项
2、Cannot resolve symbol ‘springframework’ 错误异常
解决方法: (1)第一种方法:删除Maven本地的配置文件,重新配置国内阿里源在进行重新下载Maven进行加载 阿里源文件settings.xml如下:
<!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository/path/to/local/repo--><!-- interactiveMode | This will determine whether maven prompts you when it needs input. If set to false, | maven will use a sensible default value, perhaps based on some other setting, for | the parameter in question. | | Default: truetrue--><!-- offline | Determines whether maven should attempt to connect to the network when executing a build. | This will have an effect on artifact downloads, artifact deployment, and others. | | Default: falsefalse--><!-- pluginGroup | Specifies a further group identifier to use for plugin lookup.com.your.plugins--><!-- proxy | Specification for one proxy, to be used in connecting to the network. |optionaltruehttpproxyuserproxypassproxy.host.net80local.net|some.host.com--><!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. |deploymentReporepouserrepopwd--><!-- Another sample, using keys to authenticate.siteServer/path/to/private/keyoptional; leave empty if not used.--> alimavenaliyun mavenhttp://maven.aliyun.com/nexus/content/groups/public/central<!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. |mirrorIdrepositoryIdHuman Readable Name for this Mirror.http://my.repository.com/repo/path --><!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via| or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.jdk-1.41.4jdk14Repository for JDK 1.4 buildshttp://www.myhost.com/maven/jdk14defaultalways--><!-- | Here is another profile, activated by the system property 'target-env' with a value of 'dev', | which provides a specific path to the Tomcat instance. To use this, your plugin configuration | might hypothetically look like: | | ... | | org.myco.myplugins | myplugin | | | ${tomcatPath} | | | ... | | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the inside the activation-property. |env-devtarget-envdev/path/to/tomcat/instance--><!-- activeProfiles | List of profiles that are active for all builds. |alwaysActiveProfileanotherAlwaysActiveProfile-->
(2)第二种方法:创建一个空项目,然后以新建模块的方式创建新的SpringBoot项目
以这种方式,没有感觉idea 版本和 maven版本存在冲突问题
接下来的步骤就和正常创建SpringBoot的步骤一模一样了,完成以上全部步骤之后点击启动类运行,如果能运行成功则SpringBoot项目就创建成功了。
异常案例:若创建Maven时,pom.xml文件为灰色,没有变成蓝色时,此时右键点击pom.xml文件,再点击add as maven project 稍等一会儿导入依赖就可以
说明
之所以没有配置Tomcat,是因为Spring Boot内置了Tomcat
主要的几个代码介绍
1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2、@Configuration:这是一个配置Spring的配置类;
3、@Controller:标明这是一个Spring MVC的Controller控制器;
4、main方法:在main方法中启动一个应用,即:这个应用的入口;