在Jhipster中添加applicationproperties [英] Adding applicationproperties in Jhipster

查看:97
本文介绍了在Jhipster中添加applicationproperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我这样做是通过添加

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

这是我的applicationconfig类

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;
    }

}

}

我通过使用类似

    @Inject
    private ApplicationProperties applicationProperties;

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

in classes which are need this AD IP address.

它将抛出空值

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

请帮助我,SIT即将开始,我需要为javenster创建的maven构建创建配置文件

please help me guys, SIT going to be started, I need to create a profile for maven build like jhipster created

推荐答案

我遇到了同样的问题,花了几个小时才弄清楚……Jhipster具有预先配置的属性类,用户可以自定义自己的属性:

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:

Jhipster网站上的报价:

Quote from Jhipster website:

您生成的应用程序也可以具有自己的Spring Boot属性.强烈建议这样做,因为它允许对应用程序进行类型安全的配置,以及在IDE中自动完成和编写文档.

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已在config包中生成了一个ApplicationProperties类,该类已经进行了预配置,并且在底部已记录了application.yml,application-dev.yml和application-prod.yml文件.您所需要做的就是编写自己的特定属性.

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文件中设置了属性.

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

在ApplicationProperties类中:

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;
            }
        }

    }
}

然后我将其用作:

private final ApplicationProperties.Redis redis;
public RedisConfiguration(ApplicationProperties applicationProperties){
    redis = applicationProperties.getRedis();
}

例如,使用max-waithost:

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

这篇关于在Jhipster中添加applicationproperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆