gpt4 book ai didi

java - 在 Jhipster 中添加应用程序属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:57 25 4
gpt4 key购买 nike

我正在使用 jhipster 微服务应用程序进行开发。基于用于添加应用程序特定的 jhipster 文档在这里: application-dev.ymlApplicationProperties.java

我是通过添加这个来做到这一点的

application:
mycom:
sgADIpAddress: 172.x.x.xxx

这是我的 applicationconfig 类

package com.mbb.ias.config;

import org.springframework.boot.context.properties.ConfigurationProperties;



/**
* Properties specific to JHipster.
*
* <p>
* Properties are configured in the application.yml file.
* </p>
*/
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {

private final Mycom mycom= new Mycom();



public Mycom getMycom () {
return mycom;
}



public static class Mycom {
String sgADIpAddress ="";

public String getSgADIpAddress() {
return sgADIpAddress;
}

public void setSgADIpAddress(String sgADIpAddress) {
this.sgADIpAddress = sgADIpAddress;
}

}

}

我通过使用与 jhipster 相同的属性来调用它

    @Inject
private ApplicationProperties applicationProperties;

在需要此 AD IP 地址的类中。

它会抛出空值

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

请大家帮帮我,SIT 即将开始,我需要为 maven 构建创建一个配置文件,就像创建 jhipster 一样

最佳答案

我遇到了同样的问题,花了几个小时才弄明白...Jhipster 有其预配置的属性类,用户可以自定义自己的属性:

引自Jhipster网站:

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 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.

在我的例子中,我已经在所有 yml 文件中设置了属性。

application:
redis:
host: vnode1
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
port: 6379

在 ApplicationProperties 类中:

@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;
private int maxIdle = 8;
private int minIdle = 0;


public int getMaxIdle() {
return maxIdle;
}

public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}

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-waithost:

this.redis.getPool().getMaxWait();
this.redis.getHost();

关于java - 在 Jhipster 中添加应用程序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42871453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com