闲话不扯,使用spring-boot ,建议掌握maven或是gradle。java
sample01web
1 新建一个pomspring
<?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>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.9.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project>
2 now 咱们须要搭建一个简单web程序,你只需向pom中添加apache
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3 ok 若是你使用IDE,请刷新一下工程,让maven下载依赖,如今咱们能够进行编码了。tomcat
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
4 如今能够运行mvn spring-boot:run,来启动项目了,springboot的依赖里面包括一个嵌入式的tomcat,因此咱们的项目 其实是跑在tomcat上的,浏览http://localhost:8080 ,怎么样是否是很方便。springboot
5 看上面的app
@EnableAutoConfiguration
这个是spring-boot的注解,它会帮助咱们自动发现并完成简单的spring配置。因此咱们在这个项目中没有配置的多余的 config类或是xml。maven
6 有的时候咱们要进行打包发布等工做,那么你须要向pom中添加spring-boot的插件spring-boot
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
7 执行mvn package 命令就好了。咱们获得的不是喜闻乐见的war,而是一个jar包,jar中包含了依赖的构件,包括我上面提到的嵌入式tomcat。因此咱们能够执行java -jar 来运行这个jar。gradle