Maven构建一个多模块的Spring Boot + Spring MVC项目,完全基于java config

使用Maven构建一个多模块的Spring MVC + Spring Boot项目,完全基于java config

一、新建一个maven项目,模板使用quickstart,项目名multiboot
POM.xml配置:

  4.0.0

  com.spring.boot
  multiboot
  0.0.1
  pom

  multiboot
  http://maven.apache.org

  
  	org.springframework.boot  
    spring-boot-starter-parent
  	1.3.0.RELEASE
  

  
    UTF-8
    4.2.3.RELEASE
    1.3.0.RELEASE
    8.0.28
  

  
  	multiboot1
  	multiboot2
  	multiboot3
  
  
    
      
        com.spring.boot
        multiboot1
        ${project.version}
      
      
        com.spring.boot
        multiboot2
        ${project.version}
      
      
        com.spring.boot
        multiboot3
        ${project.version}
      
      
    
  
  
    
      junit
      junit
      3.8.1
      test
    
  
  
  
    
      
        
        org.springframework.boot
        spring-boot-maven-plugin
        1.3.0.RELEASE
        
          com.spring.boot.multiboot1.App
          ZIP
        
        
          
            
              repackage
            
            
            
          
        
      
    
  


这里添加所有子模块的依赖管理 ,并把打包方式改成pom

二、在eclipse中,移除该项目中除pom.xml之外的其他所有东西

三、使用 quickstart新建maven module,分别取名 multiboot1,multiboot2,multiboot3,他们的pom.xml配置:
multiboot2的作用是使用配置Spring MVC,它的pom.xml:


  4.0.0
  
    com.spring.boot
    multiboot
    0.0.1
  
  com.spring.boot
  multiboot2
  0.0.1
  multiboot2
  http://maven.apache.org
  
    UTF-8
  
  
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
  

新建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:


  4.0.0
  
    com.spring.boot
    multiboot
    0.0.1
  
  com.spring.boot
  multiboot3
  0.0.1
  multiboot3
  http://maven.apache.org
  
    UTF-8
  
  
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
  

我们在这个模块写个业务逻辑相关的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:


  4.0.0
  
    com.spring.boot
    multiboot
    0.0.1
  
  com.spring.boot
  multiboot1
  0.0.1
  multiboot1
  http://maven.apache.org
  
    UTF-8
  
  
      
        com.spring.boot
        multiboot2
      
      
        com.spring.boot
        multiboot3
      
    
      org.springframework.boot
      spring-boot-starter-web
      ${spring.boot.version}
    
  
  
    
    multiboot1
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        1.3.0.RELEASE
      
    
  
启动模块必须依赖其他所有模块

创建 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。
搞定!!!

你可能感兴趣的:(java,web)