无法将属性注入 Spring 中的 class 引导子模块添加为 war 中的 jar

Unable to inject properties into a class in Spring boot sub module added as a jar in the war

我有一个 spring 引导项目,其中包含多个必须单独部署的模块。想要在项目之间使用通用模块,如 model、dao 和 utilities。 当我有一个项目时,我能够为 MongoDBConfiguration class 加载 mongodb 相关属性。 但是当我将 dao 作为一个单独的模块时,在服务器启动时 spring 抱怨它无法解析 MongoDBConfiguration class.

所需的属性

这是我的项目结构:

  ExampleProj
      dao
      common
      model
      webmodule_x
      module_y
      module_z
   build.gradle

除了主要 build.gradle,每个模块我都有 build.gradle。

我的 dao 模块有这样的 MongoDBConfiguration class:

@Configuration
@PropertySource(value = "classpath:/application.properties")
@EnableMongoRepositories(basePackages="com.abc.xyz.dao.repositories*" )
public class MongoDBConfiguration {

    @Value( "${mongo.host:abc12345.abc.com}" )  private String mongoHost;

    @Value( "${mongodb.name}" )  private String mongodbName;
....

    }

尝试了不同的配置,但遇到了同样的问题。 我的 webmodule_x 有 spring 启动器 class 像这样:

    @PropertySource(value={"classpath:application.properties",
                            "file:${externalDirectory}/webmodule_x/conf/application.properties"
                       } ,
                    ignoreResourceNotFound = false)
    @ComponentScan("com.abc.xyz")
    @Configuration
    @EnableAutoConfiguration
    @EnableMongoRepositories(basePackages="com.abc.xyz.dao*" )
    @Import(MongoDBConfiguration.class)
    public class SpringBootStarter {


        public static void main(String[] args) {
            ApplicationContext ctx = SpringApplication.run(SpringBootStarter.class, args);
        }

}

${mongodb.name} 属性 不知何故没有被初始化。 这是我得到的错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mongodb.name' in string value "${mongodb.name}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        ... 86 common frames omitted

这是我在 webmodule_x 中的 build.gradle,像这样:

    apply plugin: 'java'
    apply plugin: 'spring-boot'
    apply plugin: 'war'
    apply plugin: 'eclipse'
    apply plugin: 'idea'

    def tomcat_home='/usr/local/apache-tomcat-7.0.55'

    sourceCompatibility = 1.7

    buildscript {
        repositories {
            maven { url "http://mavencentral.it.abc.com:8084/nexus/content/groups/xyz" }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
        }
    }

    tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
        systemProperties = System.properties
    }
    tasks.withType(Test) {
        // get System properties needed for tests
        systemProperties['externalDirectory'] =
            System.getProperty("externalDirectory", "/opt/app/test")

        println 'externalDirectory for tests is ' +
                systemProperties['externalDirectory']
    }


     repositories {
        maven { url "http://mavencentral.it.abc.com:8084/nexus/content/groups/xyz"     }
    }
    sourceSets {
        integrationTest {
            java {
                compileClasspath += main.output + test.output
                runtimeClasspath += main.output + test.output
                srcDir file('src/integration-test/java')
            }
        resources.srcDir file('src/integration-test/resources')
        }
    }

    configurations{
        integrationTestCompile.extendsFrom compile, testCompile
        integrationTestRuntime.extendsFrom runtime, testRuntime
        providedRuntime
    }


    dependencies {
        compile 'org.springframework.boot:spring-boot-starter-web'
        compile 'org.springframework:spring-webmvc')
        compile project(':common')
        compile project(':model')
        compile project(':dao')
        testCompile('junit:junit','org.hamcrest:hamcrest-library','org.mockito:mockito-core')
        testCompile('junit:junit','org.hamcrest:hamcrest-library','org.springframework.boot:spring-boot-starter-test',
            'org.mockito:mockito-core', 'org.skyscreamer:jsonassert:1.2.3')

        integrationTestCompile('junit:junit','org.hamcrest:hamcrest-library','org.springframework.boot:spring-boot-starter-test',
            'org.mockito:mockito-core', 'org.skyscreamer:jsonassert:1.2.3')


        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

}

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    outputs.upToDateWhen { false }

}

// logback(slf4j) commons-logging
[configurations.runtime, configurations.default]*.exclude(module: 'commons-logging')

jar.enabled = true
bootRepackage.enabled = true

我的道build.gradle:

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

sourceCompatibility = 1.7
version = '1.0'
group = 'com.abc.xyz.dao'
version = '0.0.1-SNAPSHOT'

buildscript {
    repositories {
        maven { url "http://mavencentral.abc.com:8084/nexus/content/groups/xyz" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
    }
}

configurations{
    providedRuntime
}

repositories {
    maven { url "http://mavencentral.abc.com:8084/nexus/content/groups/xyz" }
}

dependencies {
    compile 'org.springframework.data:spring-data-mongodb'
    compile project(':model')
    compile 'org.springframework:spring-context'
    testCompile('junit:junit','org.hamcrest:hamcrest-library','org.mockito:mockito-core',
            'org.mockito:mockito-core', 'org.skyscreamer:jsonassert:1.2.3', 'org.springframework.boot:spring-boot-starter-test',
            'org.springframework:spring-webmvc','org.springframework.boot:spring-boot-starter-web', 'de.flapdoodle.embed:de.flapdoodle.embed.mongo:1.46.4')
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

}


jar.enabled = false
bootRepackage.enabled = false

用谷歌搜索了很多,但还没有成功。我在使用 bootRun 和 war 部署到 tomcat 时都遇到了这个错误。 请让我知道如何在初始化 dendent jar class 中的注入 bean 之前加载属性。

我确定您已经检查过了,但也许您在 prop 文件中的密钥不正确?

从你的代码中,我看到 mongo.host 然后 mongodb.name

也许 "db" 不在您的道具中?