Spring Boot Gradle多模块项目控制器映射

时间:2018-07-14 12:46:41

标签: java spring spring-boot gradle architecture

我正面临此类问题:我想在项目中使用以下架构:

-root |-core |-proj2 |-proj3

所以我有3个独立的模块。我希望模块proj2和proj3是具有唯一Controllers的外观,但是它们都应该继承于核心项目,在该项目中将放置服务和dao,以便我可以@Autowire。现在,当我将包含Application.java的{​​{1}}放在核心模块中时,Spring不会扫描其他控制器。我尝试使用@SpringBootApplication注释,但是并不能解决我的问题。 问题是,这个想法有意义吗?如果是,那么如何解决控制器的映射问题?

编辑: 更复杂的树:

@ComponentScan

build.gradle:

+demo
   |+core
      |-src
         |-main/java/com/demo/DemoApplication.java
         |-core.gradle 
   |+proj1
      |-src
         |-main/java/com/demo/controller/DemoController.java
         |-proj1.gradle
   |+proj2
   |-build.gradle

core.gradle,proj1.gradle

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

allprojects {
    apply plugin: 'java'

    group = 'com.example'
    version = '0.0.1-SNAPSHOT'

    repositories {
        mavenCentral()
    }

}

subprojects {
    apply plugin: 'org.springframework.boot'

    sourceCompatibility = 1.8

}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

project(':proj1') {
    dependencies {
        compile project(':core')
    }
}

project(':proj2') {
    dependencies {
        compile project(':core')
    }
}

DemoApplication.java

dependencies {
    compile(
            [group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion],
            [group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion],
    )
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

DemoController.java

package com.demo;

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);
    }
}

0 个答案:

没有答案