-
首先创建一个maven项目spbd(spring boot demo)
因为考虑到后面做成多模块项目,所以这儿建一个pom父项目,并删除项目下除了pom.xml的所以文件,目录结构如下:
-
在pom.xml中加入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>
<groupId>com.spbd</groupId>
<artifactId>spbd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<springboot.version>1.3.6.RELEASE</springboot.version>
</properties>
<dependencies>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
-
创建一个maven子项目spbd-core ,然后在pom.xml文件中加入spring-boot-starter-web依赖
这个模块用于封装公共的方法、引用。其它子项目都将引用这个子项目
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.spbd</groupId>
<artifactId>spbd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spbd-core</artifactId>
<name>spbd-core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 然后再创建一个子项目spbd-order,在pom.xml中加入对spbd-core的依赖
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.spbd</groupId>
<artifactId>spbd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spbd-order</artifactId>
<name>spbd-order</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.spbd</groupId>
<artifactId>spbd-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
准备工作已做好,在spbd-order中创建启动类OrderStartup
package com.spbd.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderStartup {
public static void main( String[] args) {
SpringApplication application = new SpringApplication(OrderStartup.class);
application.run(args);
}
}
上面的方式只能以jar的方式运行,如果想要在单独的容器中运行,可以让启动类继承SpringBootServletInitializer类,然后就可以打包放进tomcat等容器中运行了。
再写个controller测试一下
package com.spbd.order.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@RequestMapping("/hello")
public Object hello(){
return "hello spring boot";
}
}
然后启动OrderStartup,spring boot默认使用tomcat作为内置容器,默认端口8080 在浏览器中访问http://localhost:8080/hello 输出:hello spring boot
https://github.com/sergewu/spbd
THE END