我的Java项目无法读取application.properties文件的config,ConfigurationProperties获得null属性。当我使用代码打印类路径时:
@ComponentScan
@EnableCaching
@SpringBootApplication
@EnableAsync
@EnableScheduling
public class Application {
public static void main(String[] args) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (URL url : urls) {
System.out.println("classpath:" + url.getFile());
}
//SpringApplication app = new SpringApplication(Application.class);
//app.addListeners(new ApplicationPidFileWriter("app.pid"));
//app.run(args);
}
}
输出为:
"C:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:28450,suspend=y,server=n -javaagent:C:\Users\hlpc\.IdeaIC2018.2\system\groovyHotSwap\gragent.jar -javaagent:C:\Users\hlpc\.IdeaIC2018.2\system\captureAgent\debugger-agent.jar=file:/C:/Users/hlpc/AppData/Local/Temp/capture1.props -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.5\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\hlpc\AppData\Local\Temp\idea_classpath374768014 creditsystem.Application
Connected to the target VM, address: '127.0.0.1:28450', transport: 'socket'
classpath:/C:/Program%20Files/JetBrains/IntelliJ%20IDEA%20Community%20Edition%202018.2.5/lib/idea_rt.jar
classpath:/C:/Users/hlpc/.IdeaIC2018.2/system/groovyHotSwap/gragent.jar
classpath:/C:/Users/hlpc/.IdeaIC2018.2/system/captureAgent/debugger-agent.jar
Disconnected from the target VM, address: '127.0.0.1:28450', transport: 'socket'
Q1:为什么项目类路径中只有2个项目?
Q2:我将src目录标记为源根,当刷新gradle时,源根目录变为灰色(不是源根)!为什么?我的项目问题在哪里?我正在使用Windows 10 + Intellij Idea 2018 + Gradle 3.2.1。这是我的build.gradle:
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
jacksonVersion = '2.8.7'
springfoxVersion = '2.6.1'
poiVersion = "3.14"
aspectjVersion = '1.7.4'
}
ext['tomcat.version'] = '8.0.35'
repositories {
maven {
url 'http://repox.gtan.com:8078'
}
mavenCentral()
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7")
}
}
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
tasks.withType(ScalaCompile) {
options.encoding = "UTF-8"
}
//编译JAVA文件时采用UTF-8
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
//编译groovy代码时采用 UTF-8
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '3.2.1'
}
allprojects {
repositories {
mavenCentral()
}
}
def ccCommonBuildScript = file("$rootDir/gradle/common.gradle")
def ccDataLibs = fileTree(dir: "$rootDir/cc-data/lib", include: '*.jar')
def ccCommonDir = fileTree(dir: "$rootDir/cc-common/lib",include : '*.jar')
project(':cc-common') {
apply from: ccCommonBuildScript
description = 'credit-system-common'
dependencies {
compile project(':hl-util')
compile("com.typesafe.akka:akka-actor_$scalaLibVersion:2.4.17")
compile("com.fasterxml.jackson.module:jackson-module-scala_$scalaLibVersion:$jacksonVersion")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
compile('com.google.guava:guava:21.0')
compile("ma.glasnost.orika:orika-core:1.4.6")
compile("io.springfox:springfox-swagger2:${springfoxVersion}")
compile("io.swagger:swagger-scala-module_$scalaLibVersion:1.0.3")
compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.3'
compile group: 'commons-digester', name: 'commons-digester', version: '2.1'
compile "commons-discovery:commons-discovery:0.2"
compile "wsdl4j:wsdl4j:1.6.2"
compile "org.apache.cxf:cxf-rt-frontend-jaxws:3.1.10"
compile "org.apache.cxf:cxf-rt-transports-http:3.1.10"
compile group: 'org.apache.cxf', name: 'cxf-rt-bindings-soap', version: '3.1.10'
compile group: 'org.apache.cxf', name: 'cxf-rt-ws-addr', version: '3.1.10'
compile group: 'org.apache.cxf', name: 'cxf-rt-wsdl', version: '3.1.10'
compile group: 'org.apache.cxf', name: 'cxf-rt-ws-policy', version: '3.1.10'
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
compile "org.apache.neethi:neethi:3.0.2"
compile "org.apache.cxf:cxf:3.1.10"
compile group: 'org.codehaus.woodstox', name: 'stax2-api', version: '3.1.4'
compile group: 'xml-apis', name: 'xml-apis', version: '1.0.b2'
compile group: 'xml-resolver', name: 'xml-resolver', version: '1.2'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile("org.jsoup:jsoup:1.11.2")
compile ccCommonDir
compile("org.quartz-scheduler:quartz:2.2.1")
}
}
project(':cc-data') {
apply from: ccCommonBuildScript
description = 'credit-system-data'
dependencies {
compile project(':cc-common')
compile project(':hl-data-mybatis')
compile('org.springframework.boot:spring-boot-starter-jdbc') {
exclude group: 'org.apache.tomcat'
}
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.0'
compile('com.zaxxer:HikariCP:2.6.0')
compile('org.mongodb:bson:3.4.2')
compile("org.aspectj:aspectjweaver:1.8.10")
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
compile('org.aspectj:aspectjrt:1.8.10')
compile ccDataLibs
}
}
project(':cc-integration') {
apply from: ccCommonBuildScript
description = 'credit-system-integration'
dependencies {
compile project(':cc-data')
compile('org.springframework.boot:spring-boot-starter-web')
}
}
project(':cc-web-webservice') {
apply from: ccCommonBuildScript
description = 'credit-system-webservice'
dependencies {
compile project(':cc-business')
compile project(':cc-data')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
}
}
project(':cc-business') {
apply from: ccCommonBuildScript
description = 'credit-system-business'
configurations {
ajc
aspects
}
dependencies {
compile project(':cc-data')
compile project(':cc-integration')
compile('org.springframework.boot:spring-boot-starter-mail')
compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
compile("org.aspectj:aspectjrt:1.6.10")
ajc "org.aspectj:aspectjtools:1.6"
compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.6.12'
aspects "org.springframework:spring-aspects:3.2.2.RELEASE"
compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxws', version: '3.1.6'
compile group: 'org.apache.cxf', name: 'cxf-rt-transports-http-jetty', version: '3.1.6'
compile group: 'org.jsoup', name: 'jsoup', version: '1.10.2'
compile('com.alibaba:fastjson:1.2.35')
compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'
compile group: 'javax.transaction', name: 'jta', version: '1.1'
compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.5.1'
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.3.0'
compile group: 'org.apache.axis2', name: 'axis2-kernel', version: '1.7.6'
compile group: 'org.apache.axis2', name: 'axis2-adb', version: '1.7.6'
compile group: 'org.apache.axis2', name: 'axis2-transport-local', version: '1.7.6'
compile group: 'org.apache.axis2', name: 'axis2-transport-http', version: '1.7.6'
compile group: 'org.apache.axis2', name: 'axis2', version: '1.7.6', ext: 'pom'
}
}
/*compileJava{
options.encoding="UTF-8"
options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked" << "-Xlint:-options" << "-Xlint:unchecked"
}*/
project(':cc-api') {
apply from: ccCommonBuildScript
description = 'credit-system-api'
dependencies {
compile project(':cc-business')
compile 'io.rest-assured:json-path:3.0.1'
compile 'org.testng:testng:6.8.17'
compile('com.jayway.restassured:rest-assured:2.3.1')
testCompile 'io.rest-assured:rest-assured:3.0.1'
}
}
project(':cc-web') {
apply from: ccCommonBuildScript
description = 'credit-system-web'
dependencies {
compile project(':cc-business')
compile project(':cc-api')
compile project(':cc-common')
compile("io.springfox:springfox-swagger-ui:${springfoxVersion}")
compile 'commons-httpclient:commons-httpclient:3.1'
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.0'
compile group: 'org.springframework', name: 'spring-websocket', version: '5.0.0.RELEASE'
compile group: 'org.springframework', name: 'spring-messaging', version: '4.3.14.RELEASE'
}
}
project(':cc-web-websocket') {
apply from: ccCommonBuildScript
description = 'credit-system-websocket'
dependencies {
compile project(':cc-business')
compile project(':cc-common')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '1.5.9.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '4.3.10.RELEASE'
compile group: 'com.alibaba', name: 'druid', version: '1.1.5'
}
}
project(':cc-web-boot') {
apply from: ccCommonBuildScript
description = 'credit-system-web-boot'
jar {
baseName = "credit-system-web-boot"
}
task copyTask(type: Copy) {
from "$projectDir/build/libs/credit-system-web-boot-${version}.jar"
into "$projectDir/build/libs/api"
rename "credit-system-web-boot-${version}.jar", "credit-system-web-api-${version}.jar"
}
dependencies {
compile "org.springframework.boot:spring-boot-devtools"
compile project(':cc-web')
compile project(':cc-web-webservice')
compile project(':cc-web-websocket')
}
}
project(':cc-web-war') {
apply from: ccCommonBuildScript
apply plugin: 'war'
description = 'credit-system-web-war'
war {
baseName = "credit-system-web-war"
}
configurations {
providedRuntime
}
dependencies {
compile project(':cc-web')
compile project(':cc-web-webservice')
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
}
project(':cc-etl-sechedule') {
apply from: ccCommonBuildScript
description = 'credit-etl-sechedule'
jar {
baseName = "credit-etl-sechedule-boot"
}
dependencies {
compile project(':cc-data')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
compile("org.springframework.boot:spring-boot-starter-tomcat") {
exclude(group: 'org.apache.tomcat.embed')
}
compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
compile("io.springfox:springfox-swagger-ui:${springfoxVersion}")
}
}
答案 0 :(得分:0)
像这样指定配置文件的绝对路径:
@PropertySource(value = { "file:c:/application.properties" })