使用spring配置注释加载配置yml。我配置的4个值中的3个值一切正常。但是第4个值为空。
由于其他值正确加载,我认为没有配置错误。我一无所知......
这是我的代码:
YML-文件
spring:
profiles: test
airtable:
api-key: xxx
base: xxx
proxy: "localhost:8095"
url: testUrl
mail:
subjectPrefix: R750Explorer develop -
PropertiesClass
@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {
@NotNull
private String apiKey;
@NotNull
private String base;
private String proxy;
private String url;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public String getProxy() {
return proxy;
}
public void setProxy(String proxy) {
this.proxy = proxy;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
在这里自动发送
public class AirtableRepository {
private final org.slf4j.Logger log =
LoggerFactory.getLogger(this.getClass());
private Base base = null;
@Autowired
private AirtableProperties prop;
主要申请文件
@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableConfigurationProperties
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
.
.
.
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
所以我得到了我在api-key,base和proxy中定义的值。但是url为null。
=========================编辑===================== ======
更新。我同时使用默认的application.yml和特定于配置文件的application-test.yml
上面的yml是应用程序测试yml。继承人app.yml
spring:
application:
name: R750Explorer
boot:
admin:
#url: http://localhost:8085
devtools:
restart:
additional-paths: src, target
exclude: "**/*.log"
mail:
properties:
mail:
smp:
connectiontimeout: 5000
timeout: 3000
writetimeout: 5000
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
output:
ansi:
enabled: ALWAYS
profiles:
#default: default
#active: dev
airtable:
api-key: none-default
mail:
from-address: XXX
to-address: XXX
user: XXX
password: XXX
server:
address: 127.0.0.1
#port: 9000
compression:
enabled: true
session:
cookie:
#comment: # Comment for the session cookie.
# domain: # Domain for the session cookie.
http-only: true
# -> ein Jahr / Maximum age of the session cookie in seconds.
max-age: 31536000
#name: Session cookie name.
#path: # Path of the session cookie.
# "Secure" flag for the session cookie.
secure: true
logging:
file: logs/r750explorer.log
level:
com:
sybit: DEBUG
management:
context-path: /manage
security:
enabled: false
# roles: SUPERUSER
security:
user:
#name: admin
#password=****
现在有了线索:如果我在airtable下的默认application.yml中添加url: testurl
,它会写入值。但它在application-test.yml中有效。虽然这只是url不是代理等的情况,但它们工作正常。
答案 0 :(得分:0)
它对我有用而没有任何改变:
application.yml:
airtable:
api-key: xxx
base: xxx
proxy: localhost:8095
url: testUrl
POJO:
@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {
@NotNull
private String apiKey;
@NotNull
private String base;
private String proxy;
private String url;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public String getProxy() {
return proxy;
}
public void setProxy(String proxy) {
this.proxy = proxy;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "AirtableProperties{" +
"apiKey='" + apiKey + '\'' +
", base='" + base + '\'' +
", proxy='" + proxy + '\'' +
", url='" + url + '\'' +
'}';
}
输出:
PROPS ARE AirtableProperties{apiKey='xxx', base='xxx', proxy='localhost:8095', url='testUrl'}
答案 1 :(得分:0)
所以我找到了答案。正如我在问题中已经告诉我使用默认的application.yml和application-test.yml。
这两个文件位于单独的资源文件夹中(带有语法的test.yml的副本 - 错误位于src / resource文件夹中)。
删除Copy并实现此结构后,一切正常:
| src / resources /
| - > application.yml
| 测试/资源/
| - >应用test.yml
由于语法错误和文件的错误放置,它没有成功。感谢所有助手!