问题
I'm using jhipster microservices app for my development. Based on jhipster documentation for adding application-specific is here: application-dev.yml and ApplicationProperties.java
I did this by adding this
application:
mycom:
sgADIpAddress: 172.x.x.xxx
and this my applicationconfig class
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;
}
}
}
I've call this by using same like jhipster properties which are
@Inject
private ApplicationProperties applicationProperties;
in classes which are need this AD IP address.
it will throw null value
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
please help me guys, SIT going to be started, I need to create a profile for maven build like jhipster created
回答1:
I have the same problem and spent a couple of hours to figure it out...Jhipster has its preconfigured property class that users can customize their own properteis:
Quote from Jhipster website:
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.
In my case, I have set the properties in all yml files.
application:
redis:
host: vnode1
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
port: 6379
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;
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;
}
}
}
}
Then I use it as:
private final ApplicationProperties.Redis redis;
public RedisConfiguration(ApplicationProperties applicationProperties){
redis = applicationProperties.getRedis();
}
For instance use max-wait
and host
:
this.redis.getPool().getMaxWait();
this.redis.getHost();
回答2:
refering to this thread Spring annotation @Inject doesn't work
i remove my new operator for all classes which is calling my applicationproperties.java
@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);
}
in my ADContext i put @component on the top of my Class name, and added @Sevice annotation on the top of ADAuthenticatorService
then my
@Inject
ApplicationProperties applicationProperties;
is working flawlessly
just posting this answer so any noob like me at outside can benefit this lol
来源:https://stackoverflow.com/questions/42871453/adding-applicationproperties-in-jhipster