【发布时间】: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