【Spring boot helloworld】

14天阅读挑战赛

1.1 介绍
自从 structs2 出现上次的漏洞以后,对 spring 的关注度开始越来越浓。
以前 spring 开发需要配置一大堆的 xml,后台 spring 加入了 annotaion,使得 xml 配置简化了很多,当然
还是有些配置需要使用 xml,比如申明 component scan 等。
Spring 开了一个新的 model spring boot,主要思想是降低 spring 的入门,使得新手可以以最快的速度让程
序在 spring 框架下跑起来。
那么如何写 Hello world 呢?
Hello 之步骤 :
(1) 新建一个 Maven Java 工程
(2) pom.xml 文件中添加 Spring Boot Maven 依赖
(3) 编写启动类
(4) 运行程序
1.2 Hello New
这个步骤很简单,相比大家都会,小编在此为了文档的完整性,稍作简单说明:
首先使用 IDE Eclipse,MyEclipse )工具新建一个 Maven 工程,可以是 Maven Java Project, 也可以是 Maven
Web Project, 随便取一个工程名称。我使用的是 STS ,工程名是 spring-boot-hello1
1.3 Hello Maven
第二步,在 pom.xml 中引入 spring-boot-start-parent,spring 官方的解释叫什么 stater poms, 它可以提供
dependency management, 也就是说依赖管理,引入以后在申明其它 dependency 的时候就不需要 version 了,
后面可以看到。
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version>1.3.3.RELEASE</version>
</parent>
1.4 Hello maven web
第三步,因为我们开发的是 web 工程,所以需要在 pom.xml 中引入 spring-boot-starter-web,spring 官方解
释说 spring-boot-start-web 包含了 spring webmvc tomcat web 开发的特性。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1.5 Hello Maven Run Application
如果我们要直接 Main 启动 spring,那么以下 plugin 必须要添加,否则是无法启动的。如果使用 maven 的
spring-boot:run 的话是不需要此配置的。(我在测试的时候,如果不配置下面的 plugin 也是直接在 Main 中运行
的。)
< build >
< plugins >
< plugin >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-maven-plugin </ artifactId >
</ plugin >
</ plugins >
</ build >
1.6 Hello 之 coding
第四步,真正的程序开始啦,我们需要一个启动类,然后在启动类申明让 spring boot 自动给我们配置 spring
需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问 hello
world 字样的例子:
@RestController
@SpringBootApplication
public class App {
@RequestMapping ( "/" )
public String hello(){
return "Hello world!" ;
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
其中 @SpringBootApplication 申明让 spring boot 自动给程序进行必要的配置,等价于以默认属性使用
@Configuration @EnableAutoConfiguration @ComponentScan
@RestController 返回 json 字符串的数据,直接可以编写 RESTFul 的接口;
1.7 Hello Run
第五步,就是运行我们的 Application 了,我们先介绍第一种运行方式。第一种方式特别简单:右键 Run As
-> Java Application 。之后打开浏览器输入地址: http://127.0.0.1:8080/ 就可以看到 Hello world! 了。第二种方式
右键 project – Run as – Maven build – Goals 里输入 spring-boot:run , 然后 Apply, 最后点击 Run
1.8 Hello Error
顺利的情况下当然是皆大欢喜了,但是程序吧往往会给你开个小玩笑。那么我们要注意什么呢?主要是 jdk
的版本之类的,请看官方说明:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

God Zhang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值