Spring Boot 快速入门(一)
简介
相信很多人都接触spring框架很长时间了,每次搭建spring框架的时候都需要配置好多的jar、xml,做很多繁琐重复的配置,稍微不留神就会出现各种各样的问题,每次调试真的是香菇、蓝瘦啊。
spring boot的出现帮助我们彻底解决了这些jar的依赖,只需要很少的配置就可以完成我们的开发工作,我们可以把自己的应用打包成jar,使用java -jar来运行spring web应用,spring boot集成了很多的web容器,后面都会慢慢讲到这些,今天我们就开始使用spring boot完成一个最简单的web应用。
使用maven构建项目
spring官网提供了了一个非常方便的地址来帮助我们创建spring boot项目,http://start.spring.io/
,输入相关参数点击 Generate Project 即可下载自动生成的压缩包。
该项目采用maven构建,加压后直接使用自己的IDE导入maven工程即可
1,打开pom文件就可以看到新的项目比传统的spring mvc项目的pom文件接口清晰了很多
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.1.RELEASE</version>
- </parent>
第一个地方是多了上面的代码,这个spring boot会自动帮我门引入一些约定的配置,查看源码得知
- <?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-dependencies</artifactId>
- <version>1.4.1.RELEASE</version>
- <relativePath>../../spring-boot-dependencies</relativePath>
- </parent>
- <artifactId>spring-boot-starter-parent</artifactId>
- <packaging>pom</packaging>
- <name>Spring Boot Starter Parent</name>
- <description>Parent pom providing dependency and plugin management for applications
- built with Maven</description>
- <url>http://projects.spring.io/spring-boot/</url>
- <organization>
- <name>Pivotal Software, Inc.</name>
- <url>http://www.spring.io</url>
- </organization>
- <properties>
- <java.version>1.6</java.version>
- <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <maven.compiler.source>${java.version}</maven.compiler.source>
- <maven.compiler.target>${java.version}</maven.compiler.target>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- <exclusions>
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <!-- Turn on filtering by default for application properties -->
- <resources>
- <resource>
- <directory>${basedir}/src/main/resources</directory>
- <filtering>true</filtering>
- <includes>
- <include>**/application*.yml</include>
- <include>**/application*.properties</include>
- </includes>
- </resource>
- <resource>
- <directory>${basedir}/src/main/resources</directory>
- <excludes>
- <exclude>**/application*.yml</exclude>
- <exclude>**/application*.properties</exclude>
- </excludes>
- </resource>
- </resources>
- <pluginManagement>
- <plugins>
- <!-- Apply more sensible defaults for user projects -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-failsafe-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>integration-test</goal>
- <goal>verify</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>${start-class}</mainClass>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <includes>
- <include>**/*Tests.java</include>
- <include>**/*Test.java</include>
- </includes>
- <excludes>
- <exclude>**/Abstract*.java</exclude>
- </excludes>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <configuration>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- <archive>
- <manifest>
- <mainClass>${start-class}</mainClass>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <configuration>
- <mainClass>${start-class}</mainClass>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <delimiters>
- <delimiter>${resource.delimiter}</delimiter>
- </delimiters>
- <useDefaultDelimiters>false</useDefaultDelimiters>
- </configuration>
- </plugin>
- <plugin>
- <groupId>pl.project13.maven</groupId>
- <artifactId>git-commit-id-plugin</artifactId>
- <version>2.1.11</version>
- <executions>
- <execution>
- <goals>
- <goal>revision</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <verbose>true</verbose>
- <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
- <generateGitPropertiesFile>true</generateGitPropertiesFile>
- <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
- </configuration>
- </plugin>
- <!-- Support our own plugin -->
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <mainClass>${start-class}</mainClass>
- </configuration>
- </plugin>
- <!-- Support shade packaging (if the user does not want to use our plugin) -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>1.4.1.RELEASE</version>
- </dependency>
- </dependencies>
- <configuration>
- <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
- <createDependencyReducedPom>true</createDependencyReducedPom>
- <filters>
- <filter>
- <artifact>*:*</artifact>
- <excludes>
- <exclude>META-INF/*.SF</exclude>
- <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude>
- </excludes>
- </filter>
- </filters>
- </configuration>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/spring.handlers</resource>
- </transformer>
- <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
- <resource>META-INF/spring.factories</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/spring.schemas</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>${start-class}</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </project>
spring boot默认帮我引入的jdk版本是1.6,如果需要改动,则在自己pom文件中直接重写<java.version>1.6</java.version>即可。
还可以看到它已经帮我们引入了spring-core和commons-logging包
继续往下看可以发现一些属性文件他也帮我定义好了规则,不需要我们自己在xml中定义了,已经一些maven插件的定义这里就不一一说明 ,具体插件是做什么用的,各位可以看下maven的相关知识。
第二个改变的地方是
- <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>
- spring-boot-starter:核心模块,包括自动配置支持、日志
- spring-boot-starter-test :测试模块,包括JUnit、Hamcrest、Mockito
- spring-boot-starter-web :开发web项目依赖
- 代码编写
说了这么多的配置,来让我们创建第一个class吧
- @Controller
- @EnableAutoConfiguration
- @RequestMapping("/ctr")
- public class SampleController
- {
- @RequestMapping("/getStr")
- @ResponseBody
- String home(){
- return "Hello World";
- }
- @RequestMapping("/info")
- @ResponseBody
- public Map<String,String> getInfo(){
- Map<String, String> map = new HashMap<>();
- map.put("name", "jack");
- return map;
- }
- public static void main(String[] args)
- {
- SpringApplication.run(SampleController.class,args);
- }
- }
到此为止,我们的第一个spring 的web项目就结束了,直接运行main方法,浏览器中输入:http://127.0.0.1:8080/ctr/info 可以看到运行结果 {"name": "jack"}
怎么样,是不是很神奇,没有启动任何的web容器,只需要执行main方法就可以运行我们的web项目了,而且配置就这么简单任性。
Spring Boot 快速入门(一)的更多相关文章
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 快速入门 史上最简单
1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...
- Spring Boot 快速入门(IDEA)
从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...
- 笔记61 Spring Boot快速入门(一)
IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...
- Spring Boot 快速入门笔记
Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- Spring Boot快速入门(最新)
本章通过完成Spring Boot基础项目的构建并实现一个简单的Http请求处理,让大家对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性.预计阅读及演练过程将花费约5分钟. ...
- Spring Boot快速入门
安装 安装依赖 maven是一个依赖管理工具,我们利用maven进行构建.创建一个maven项目,在pom.xml里面添加依赖项 <?xml version="1.0" en ...
随机推荐
- smarty的学习计划(1)
1.什么事smarty? 不知道,smarty是一个使用PHP写出来的模板引擎,它提供了逻辑外在内容的分离 2.smarty优点: a.速度:成熟的模板引擎技术 b.编译型:采用smarty编写的程序 ...
- 习惯的PHP命名规则
从C++转PHP也已经很长一段时间了,一直有点代码洁癖,对于文件名,接口名,类名,方法名等都使用严格的驼峰命名法, 但是有时候会犹豫到底用首字母大写区分还是用下划线区分.今天简单总结和规约一下. 1 ...
- swift MBProgressHUD加载gif或者apng的动图
效果图 给MBProgressHUD添加一个分类(extension) extension MBProgressHUD { /// MBProgressHUD 显示加载gif hud方法 /// // ...
- oracle数据库常用的99条查询语句(转载)
1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...
- RPi WiringPi安装使用
sudo apt-get install git-core git clone git://git.drogon.net/wiringPi cd wiringPi ./build 使用Exam ...
- Ehcache 整合Spring 使用页面、对象缓存(1)
转自:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以 ...
- javaWeb学习总结(11)- 监听器(Listener)学习
一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其 实就是一个实现特定接口的普 ...
- Asp.Net Core轻量级Aop解决方案:AspectCore
什么是AspectCore Project ? AspectCore Project 是适用于Asp.Net Core 平台的轻量级 Aop(Aspect-oriented programming) ...
- 新写的高仿Arcmap,要的拿去玩玩
本想着对所学的ArcGIS Engine开发作一个了结,于是乎写了这么一个仿照Arcmap的程序.我所见过的地理信息系统中,ArcGIS是功能最完善.二次开发最易上手的平台了(当然别提AutoCAD那 ...
- 【小练习05】HTML+CSS--淘宝商铺小页面
要求实现如下效果图: 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...