【问题标题】:Unable to inject properties into a class in Spring boot sub module added as a jar in the war无法将属性注入到在战争中添加为 jar 的 Spring boot 子模块中的类中
【发布时间】:2015-08-29 22:50:43
【问题描述】:

我有一个 Spring Boot 项目,其中包含多个必须单独部署的模块。想要在项目之间使用模型、dao 和实用程序等通用模块。 当我有一个项目时,我能够为 MongoDBConfiguration 类加载与 mongodb 相关的属性。 但是当我将 dao 作为一个单独的模块时,在服务器启动时 spring 抱怨它无法解析 MongoDBConfiguration 类所需的属性。

这是我的项目结构:

  ExampleProj
      dao
      common
      model
      webmodule_x
      module_y
      module_z
   build.gradle

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

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

@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 boot starter 类:

    @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$2.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

我的 dao 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 和将战争部署到 tomcat 时都收到此错误。 请让我知道如何在初始化 dendent jar 类中的注入 bean 之前加载属性。

【问题讨论】:

    标签: spring spring-mvc spring-data-mongodb


    【解决方案1】:

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

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

    也许“db”不在你的道具中?

    【讨论】:

    • 我已验证,但它确实有正确的属性名称。感谢您指出我们不一致的命名。如果您有任何其他线索,请告诉我。谢谢。
    • 其实你知道吗,我的属性文件中有一个错字。这太荒谬了!
    • 哇!!它不一致,它与 bootRun 一起工作,但在 tomcat 中部署为 war 文件时不起作用。在此处读取依赖文件(如 MongoDBConfiguration)中的外部属性时需要某人的帮助。
    • 展开你的战争文件。属性文件在吗?如果是,它在哪里?哪个文件夹?
    • 我试图从具有系统属性的外部文件中检索值。我通过修复系统属性解决了它。感谢您对此进行调查。
    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 2019-09-30
    • 2016-08-16
    • 2017-04-12
    • 1970-01-01
    • 2023-04-04
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多