ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-在Jhipster中添加applicationproperties

2019-11-11 16:19:57  阅读:497  来源: 互联网

标签:jhipster spring-boot spring java


我正在使用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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有