[英]Dynamically change bean using applicationProperties in SpringBoot
[英]Adding applicationproperties in Jhipster
我正在使用jhipster微服務應用程序進行開發。 基於jhipster文檔,用於添加特定於應用程序的文檔: application-dev.yml和ApplicationProperties.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即將開始,我需要創建一個像jhipster創建的maven構建的配置文件
我有同樣的問題,花了幾個小時來搞清楚... Jhipster有預先配置的屬性類,用戶可以自定義自己的屬性:
來自Jhipster網站的報價:
生成的應用程序也可以擁有自己的Spring Boot屬性。 強烈建議這樣做,因為它允許應用程序的類型安全配置,以及IDE中的自動完成和文檔。
JHipster在配置包中生成了一個ApplicationProperties類,該類已經預配置,並且已經在application.yml,application-dev.yml和application-prod.yml文件的底部記錄。 您需要做的就是編寫自己的特定屬性。
在我的例子中,我已經在所有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-wait
和host
:
this.redis.getPool().getMaxWait();
this.redis.getHost();
引用此線程Spring注釋@Inject不起作用
我為所有調用我的applicationproperties.java的類刪除了我的new運算符
@Service
public class ADAuthenticatorService {
private static final Logger log = LoggerFactory.getLogger(ADAuthenticatorService.class);
private final static long DIFF_NET_JAVA_FOR_DATE_AND_TIMES = 11644473600000L;
@Inject
ADContext adContext;
/**
* AD authentication
*
* @param UserID,
* AD User ID
* @param Password,
* AD Password
* @return ADProfile
*/
@Inject
ApplicationProperties applicationProperties;
public ADProfile authenticate(String UserID, String Password) throws Exception {
ADContext context = adContext.getDefaultContext(applicationProperties);
return authenticate(context, UserID, Password);
}
在我的ADContext中,我將@component放在我的類名的頂部,並在ADAuthenticatorService的頂部添加了@Sevice注釋
然后我的
@Inject
ApplicationProperties applicationProperties;
工作完美無瑕
只是發布這個答案所以任何像我這樣的noob在外面可以受益這個哈哈
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.