您的位置:首页 > 编程语言

不写一行代码即可运行一个应用

2016-11-19 14:32 483 查看
软件工程的构件化道路发展到今天,已经具有成熟的技术。这就像今天的大厦建筑一样,使用预制的构件,就能建造万丈高楼。同样道理,使用Spring Boot开发框架,也可以支持这种构件化的功能,正像其官方网站所介绍的那样,你甚至可以不用写一行代码,就可运行一个应用:“Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.” (http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-introducing-spring-boot.html)。

下面使用IntelliJ IDEA工具,按照下列步骤,来见证一下Spring Boot在软件工程构件化中所表现的优越能力。

1.新建项目,选择Spring Initalizr,如下图:

<?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>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


View Code
而在源程序目录src下面则生成了一个主程序:DemoApplication.java,工程正是使用这个主程序来启动应用的,程序代码如下所示:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}


6. 增加一个Spring Boot运行配置,选择生成的主程序:DemoApplication,如下图:



7. 运行应用,如下图所示,可以看到启动了Tomcat服务,并打开了8080端口。



通过8080端口在浏览器中打开链接:http://localhost:8080/,如下图所示。当然,这个时候,除了一个错误提示之外,什么也看不到,这不奇怪,因为我们并没有做什么,除了在上面过程中输入过“demo”这四个英文字母之外,确实未曾写过一个字符的代码。但是,不管怎么说,这个应用是正常运行的,这就足够了。



哇,不错!如果你会为此发出一声称赞,那么恭喜你,说明你童心未泯,在这个世界中,你将具有很强的竞争力。如果你对此不屑一顾,那么同样恭喜你,这说明你已经是一介高人了呀。但是不管持什么心态,多一份了解,总是能让你所属的天空更加广阔而美丽。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐