我使用的是spring-boot-starter-web最新版本2.2.6.RELEASE,但我的应用程序总是为带有@value注释的变量返回null。
我的build.gradle
plugins {
id 'com.google.cloud.tools.jib' version '2.1.0'
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'eclipse'
id 'idea'
id 'maven-publish'
}
jar {
archiveBaseName = 'my-project'
project.version = '1.0.0'
}
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
dependencies {
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '+'
implementation('org.springframework.boot:spring-boot-starter-web:+') {
exclude module: 'spring-boot-starter-tomcat'
}
implementation("org.springframework.boot:spring-boot-starter-jetty:+")
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0'
implementation group: 'io.kubernetes', name: 'client-java', version: '+'
implementation group: 'com.google.cloud', name: 'google-cloud-dataproc', version: '+'
implementation(project(':my-project-1')) {
exclude group: 'org.yaml'
}
implementation project(':my-project2')
}
tasks.withType(JavaCompile) {
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true
}我的Java代码,
@Component
public class MyClass {
@Value("${dataproc.host}")
private static String dataprocEndpoint;我的application.yml
dataproc:
host: dataproc.googleapis.com:443我的Application.java
@SpringBootApplication
@EnableSwagger2
@Configuration
public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
private transient String PATTERNS_TO_HIDE = "^/(?!error|autoconfig|beans|profile|info|health|pause|env|refresh|resume|restart|actuator|dump|mappings|trace|metrics|heapdump|archaius|configprops|features|liquibase|loggers|auditevents).*$";
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Docket api() {
ApiInfo apiInfo = new ApiInfoBuilder().license("Proprietary").title("My Application")
.description("My Application").build();
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex(PATTERNS_TO_HIDE)).build().pathMapping("/").apiInfo(apiInfo)
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, newArrayList(new ResponseMessageBuilder().code(500)
.message("500 message").responseModel(new ModelRef("Error")).build()));
}
}启动服务器时总是得到null,dataprocEndpoint: null
我这里遗漏了什么配置?
请在此处提供您的输入。
发布于 2020-04-22 21:34:26
数据过程:主机:'dataproc.googleapis.com:443'
Yml处理器将配置属性名称解释为dataproc.host.dataproc.googleapis.com.443
@Value("${dataproc.host}")私有字符串dataprocEndpoint;
静态字段使用类加载进行初始化。Spring bean后处理器不能在这么早的阶段注入值。如果你想给静态字段注入值,你可以使用setter注入。
参考https://mkyong.com/spring/spring-inject-a-value-into-static-variables/
https://stackoverflow.com/questions/61366335
复制相似问题