2015-08-29 12 views
1

我有一個具有多個模塊的彈簧引導項目,必須單獨部署。希望在項目之間使用通用模塊,如模型,道路和實用程序。 當我有一個項目時,我能夠獲得爲MongoDBConfiguration類加載的mongodb相關屬性。 但是當我將dao作爲一個單獨的模塊時,在服務器啓動時,spring會抱怨說它無法解析MongoDBConfiguration類所需的屬性。無法在Spring Boot子模塊中的類中注入屬性作爲戰爭中的jar添加

這裏是我的凸出結構:

ExampleProj 
     dao 
     common 
     model 
     webmodule_x 
     module_y 
     module_z 
    build.gradle 
從主要的build.gradle

除此之外,我對每個模塊的build.gradle。

吾道模塊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有春天開機啓動類是這樣的:

@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 

吾道的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中都遇到了這個錯誤。 請讓我知道如何加載屬性之前註冊的bean在一個dendent jar類被初始化。

回答

0

我確定你已經檢查過了,但是你的密鑰在prop文件中是不正確的?

從你的代碼,我看到mongo.host然後蒙戈分貝。名稱

也許「DB」是不是在你的道具?

+0

我確認了,但它確實有正確的屬性名稱。感謝您指出我的不一致的命名。讓我知道你是否有其他線索。謝謝。 –

+0

其實你知道什麼,我的財產文件中有一個錯字。這是荒唐的!! –

+0

Aww !!它不一致,它與bootRun一起工作,但在tomcat中作爲war文件部署時不起作用。需要某人幫助讀取像MongoDBConfiguration這樣的依賴文件中的外部屬性。 –

相關問題