使用Maven构建一个多模块的Spring MVC + Spring Boot项目,完全基于java config
一、新建一个maven项目,模板使用quickstart,项目名multiboot POM.xml配置:
这里添加所有子模块的依赖管理 ,并把打包方式改成pom
二、在eclipse中,移除该项目中除pom.xml之外的其他所有东西
三、使用 quickstart新建maven module,分别取名 multiboot1,multiboot2,multiboot3,他们的pom.xml配置: multiboot2的作用是使用配置Spring MVC,它的pom.xml:
新建Spring MVC配置类:
multiboot3模块可以是我们处理业务的模块,当然这样的模块会建很多, 它的pom.xml:
我们在这个模块写个业务逻辑相关的Controller:
multiboot1模块的作用是使用spring boot启动项目,它的pom.xml:
创建 Spring Boot 启动类:(当然eclipse已经给自动生成了一个)
此时开发框架已经搭建完毕,开始编译打包项目吧。 打开命令行,进入项目根目录,输入命令:mvn clean install,然后会发现项目的包存放在multiboot\multiboot1\target下。 继续输入命令启动服务:java -jar multiboot1/targetmultiboot1.jar --server.port=9000, 这里的端口号可以随意设置,只要没被占用。 此时项目已经启动了,打开浏览器,输入 http://127.0.0.1:9000/hello3,页面成功显示hello world。 搞定!!!
一、新建一个maven项目,模板使用quickstart,项目名multiboot POM.xml配置:
<project xmlns="http://maven.apache/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>multiboot</name>
<url>http://maven.apache</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<spring.boot.version>1.3.0.RELEASE</spring.boot.version>
<tomcat.version>8.0.28</tomcat.version>
</properties>
<modules>
<module>multiboot1</module>
<module>multiboot2</module>
<module>multiboot3</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot3</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Override Spring Data release train provided by Spring Boot
使用这个可以不用去继承spring-boot-starter-parent
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency> -->
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- The plugin rewrites your manifest -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.RELEASE</version>
<configuration><!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.spring.boot.multiboot1.App</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
<!--可以生成不含依赖包的不可执行Jar包-->
<!-- configuration>
<classifier>exec</classifier>
</configuration> -->
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这里添加所有子模块的依赖管理 ,并把打包方式改成pom
二、在eclipse中,移除该项目中除pom.xml之外的其他所有东西
三、使用 quickstart新建maven module,分别取名 multiboot1,multiboot2,multiboot3,他们的pom.xml配置: multiboot2的作用是使用配置Spring MVC,它的pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache/POM/4.0.0"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot2</artifactId>
<version>0.0.1</version>
<name>multiboot2</name>
<url>http://maven.apache</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope><!-- 编译需要而发布不需要的jar包 -->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
新建Spring MVC配置类:
package com.spring.boot.multiboot2;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.spring.boot")
public class WebConfig extends WebMvcConfigurerAdapter {
}
这里的@ComponentScan(basePackages="com.spring.boot")会自动扫描这个package下的注解
multiboot3模块可以是我们处理业务的模块,当然这样的模块会建很多, 它的pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache/POM/4.0.0"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot3</artifactId>
<version>0.0.1</version>
<name>multiboot3</name>
<url>http://maven.apache</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
我们在这个模块写个业务逻辑相关的Controller:
package com.spring.boot.multiboot3;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class App3Controller {
@RequestMapping("/hello")
public String hello3() {
return "hello world";
}
}
multiboot1模块的作用是使用spring boot启动项目,它的pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache/POM/4.0.0"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot1</artifactId>
<version>0.0.1</version>
<name>multiboot1</name>
<url>http://maven.apache</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot2</artifactId>
</dependency>
<dependency>
<groupId>com.spring.boot</groupId>
<artifactId>multiboot3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<build>
<!-- 为jar包取名 -->
<finalName>multiboot1</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
启动模块必须依赖其他所有模块
创建 Spring Boot 启动类:(当然eclipse已经给自动生成了一个)
package com.spring.boot.multiboot1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import com.spring.boot.multiboot2.WebConfig;
//此注解会从当前包向下扫描注解,因此Main Class尽量放在根目录,比如com.spring.boot,或com.spring,这样可以保证所有包都能被扫描到
@SpringBootApplication
//如果把该Main Class所在包放在WebConfig.class之上,比如com.spring.boot,就不需要@Import了
@Import(WebConfig.class)
public class App {
public static void main(String[] args){
SpringApplication.run(App.class, args);
}
}
此时开发框架已经搭建完毕,开始编译打包项目吧。 打开命令行,进入项目根目录,输入命令:mvn clean install,然后会发现项目的包存放在multiboot\multiboot1\target下。 继续输入命令启动服务:java -jar multiboot1/targetmultiboot1.jar --server.port=9000, 这里的端口号可以随意设置,只要没被占用。 此时项目已经启动了,打开浏览器,输入 http://127.0.0.1:9000/hello3,页面成功显示hello world。 搞定!!!
更多推荐
Maven构建一个多模块的Spring Boot + Spring MVC项目,完全基于java config
发布评论