编程语言
首页 > 编程语言> > java-在Jhipster中添加applicationproperties

java-在Jhipster中添加applicationproperties

作者:互联网

我正在使用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即将开始,我需要为javenster创建的Maven构建创建配置文件

解决方法:

我遇到了同样的问题,花了几个小时才弄清楚……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-wait和host:

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

标签:jhipster,spring-boot,spring,java
来源: https://codeday.me/bug/20191111/2020913.html