在哪里以及如何定义应用程序属性? -JHIpster [英] Where and How To Define An Application Property? - JHIpster
问题描述
在Spring Boot中,可以在application.properties文件中定义应用程序属性.例如,可以将Rest的前缀定义为
In Spring Boot, an application property can be defined in application.properties file. For example, a prefix of Rest can be defined as
spring.data.rest.basePath=api
对于基于Spring Boot的JHipster,我猜可以在application.yml文件中定义一个应用程序属性.但是,以下任何一种方法都不适合我:404错误.
For JHipster which is based on Spring Boot, I guess that an application property could defined in the application.yml file. But none of the follow approach work for me: a 404 error.
spring.data.rest.basePath: api
spring:
data:
rest:
basePath: api
另一种可能性是配置本身不起作用.
The other possibility is the configuration itself doesn't work.
推荐答案
我遇到了同样的问题,终于解决了!
I have same problem and finally figured it out!
Jhipster网站上的报价:
Quote from Jhipster website:
您生成的应用程序也可以具有自己的Spring Boot属性.强烈建议这样做,因为它允许对应用程序进行类型安全的配置,以及在IDE中自动完成和编写文档.
Your generated application can also have its own Spring Boot properties. This is highly recommended, as it allows type-safe configuration of the application, as well as auto-completion and documentation within an IDE.
JHipster已在config包中生成了一个ApplicationProperties类,该类已经进行了预配置,并且在底部已记录了application.yml,application-dev.yml和application-prod.yml文件.您所需要做的就是编写自己的特定属性.
JHipster has generated a ApplicationProperties class in the config package, which is already preconfigured, and it is already documented at the bottom the application.yml, application-dev.yml and application-prod.yml files. All you need to do is code your own specific properties.
就我而言,我已在applicaiton-prod.yml中设置了属性
In my case, I have set the properties in applicaiton-prod.yml
application:
redis:
host: vnode1
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
port: 6379
在ApplicationProperties类中:
In ApplicationProperties class:
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {
public final Redis redis = new Redis();
public Redis getRedis() {
return redis;
}
public static class Redis {
private String host = "127.0.0.1";
private int port = 0;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
private Pool pool = new Pool();
public void setPool(Pool pool) {
this.pool = pool;
}
public Pool getPool() {
return this.pool;
}
public static class Pool {
private int maxActive = 8;
private int maxWait = -1;
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
private int maxIdle = 8;
private int minIdle = 0;
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxActive() {
return maxActive;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
}
}
}
然后我将其用作:
private final ApplicationProperties.Redis redis;
public RedisConfiguration(ApplicationProperties applicationProperties){
redis = applicationProperties.getRedis();
}
例如,使用max-wait
和host
:
this.redis.getPool().getMaxWait();
this.redis.getHost();
希望有帮助.
这篇关于在哪里以及如何定义应用程序属性? -JHIpster的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!