在 Spring Boot 中,可以在 application.properties 文件中定义应用程序属性。例如,Rest 的前缀可以定义为
spring.data.rest.basePath=api
对于基于 Spring Boot 的 JHipster,我猜想可以在 application.yml 文件中定义应用程序属性。但以下方法都不适合我:404 错误。
spring.data.rest.basePath: api
spring:
data:
rest:
basePath: api
另一种可能性是配置本身不起作用。
我有同样的问题,终于弄清楚了!
引用自 Jhipster 网站:
您生成的应用程序还可以拥有自己的 Spring Boot 属性。强烈建议这样做,因为它允许对应用程序进行类型安全配置,以及 IDE 中的自动完成和文档记录。
JHipster 在配置包中生成了一个 ApplicationProperties 类,该类已预先配置,并且已记录在 application.yml、application-dev.yml 和 application-prod.yml 文件的底部。您所需要做的就是编写您自己的特定属性的代码。
就我而言,我已在 application-prod.yml 中设置属性
application:
redis:
host: vnode1
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
port: 6379
在应用程序属性类中:
@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
and host
:
this.redis.getPool().getMaxWait();
this.redis.getHost();
希望能帮助到你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)