Spring boot 入门 ---(一)

环境 JavaSDKv1.6或更高版本。

这里使用maven作为构建工具

下面是一个典型的pom.xml文件:

配置pom.xml文件

<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht tp://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>  
  <groupId>com.example</groupId>  
  <artifactId>myproject</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <!--	Inherit	defaults from Spring Boot -->  
  <parent> 
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>1.4.0.BUILD-SNAPSHOT</version> 
  </parent>  
  <!--    Add typical dependencies    for a   web application -->  
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId>  
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
  </dependencies>  
  <!--	Package	as	an	executable	jar	-->  
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build>  
  <!--	Add	Spring	repositories	-->  
  <!--	(you don't need	this if	you are	using a	.RELEASE versio n) -->  
  <repositories> 
    <repository> 
      <id>spring-snapshots</id>  
      <url>http://repo.spring.io/snapshot</url>  
      <snapshots>
        <enabled>true</enabled>
      </snapshots> 
    </repository>  
    <repository> 
      <id>spring-milestones</id>  
      <url>http://repo.spring.io/milestone</url> 
    </repository> 
  </repositories>  
  <pluginRepositories> 
    <pluginRepository> 
      <id>spring-snapshots</id>  
      <url>http://repo.spring.io/snapshot</url> 
    </pluginRepository>  
    <pluginRepository> 
      <id>spring-milestones</id>  
      <url>http://repo.spring.io/milestone</url> 
    </pluginRepository> 
  </pluginRepositories> 
</project>


spring-boot-starter-parent是使用Spring Boot的一种不错的方式,但它 并不总是最合适的。有时你可能需要继承一个不同的父POM,或只是不喜欢我们 的默认配置,那你可以使用import作用域这种替代方案:

编写代码

为了完成应用程序,我们需要创建一个单独的Java文件。Maven默认会编 译 src/main/java 下的源码,所以你需要创建那样的文件结构,并添加一个名 为 src/main/java/Example.java 的文件:

import	org.springframework.boot.*;
	@RequestMapping("/hello")
	public String hello(){
		return "hello springBoot!";
	}
	public static void main(String[] args) {
		SpringApplication.run(HelloSpringBoot.class,args);
	}
}


运行示例

到此,示例应用可以工作了。由于使用了 spring-boot-starter-parent POM,这样我们就有了一个非常有用的run目标来启动程序。在项目根目录下输 入 mvn spring-boot:run 启动应用:
$ mvn spring-boot:run
. ____ _ 	__	_	\	\	\	\ (	(	)\___	|	'_	|	'_|	|	'_	\/	_`	|	\	\	\	\	\\/		___)|	|_)|	|	|	|	|	||	(_|	|		)	)	)	)		'		|____|	.__|_|	|_|_|	|_\__,	|	/	/	/	/	=========|_|==============|___/=/_/_/_/	::	Spring	Boot	::		(v1.4.1.BUILD-SNAPSHOT) .......	.	.	. .......	.	.	.	(log	output	here) .......	.	.	. ........	Started	Example	in	2.222	seconds	(JVM	running	for	6.514 )
如果使用浏览器打开localhost:8080,你应该可以看到如下输出:
Hello World!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘彦青-Yannis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值