在 Jhipster 中添加应用程序属性

Han*_*Nan 5 java spring spring-boot jhipster

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

我通过添加这个来做到这一点

application:
      mycom:
        sgADIpAddress: 172.x.x.xxx    
Run Code Online (Sandbox Code Playgroud)

这是我的 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

}

我通过使用相同的 jhipster 属性来调用它

    @Inject
    private ApplicationProperties applicationProperties;
Run Code Online (Sandbox Code Playgroud)

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

它会抛出空值

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

请帮帮我,SIT 即将开始,我需要为 maven 构建创建一个配置文件,就像 jhipster 创建的一样

Hai*_*ang 5

我有同样的问题,花了几个小时才弄明白……Jhipster 有它的预配置属性类,用户可以自定义他们自己的属性:

引用自 Jhipster 网站:

您生成的应用程序也可以有自己的 Spring Boot 属性。这是强烈推荐的,因为它允许应用程序的类型安全配置,以及 IDE 中的自动完成和文档。

JHipster 已经在 config 包中生成了一个 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
Run Code Online (Sandbox Code Playgroud)

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

    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将其用作:

private final ApplicationProperties.Redis redis;
public RedisConfiguration(ApplicationProperties applicationProperties){
    redis = applicationProperties.getRedis();
}
Run Code Online (Sandbox Code Playgroud)

例如使用max-waithost

this.redis.getPool().getMaxWait();
this.redis.getHost();
Run Code Online (Sandbox Code Playgroud)


Han*_*Nan 2

引用这个线程 Spring注释@Inject不起作用

我删除了所有调用我的 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);

}
Run Code Online (Sandbox Code Playgroud)

在我的 ADContext 中,我将 @component 放在类名的顶部,并在 ADAuthenticatorService 的顶部添加 @Sevice 注释

然后我的

@Inject
ApplicationProperties applicationProperties;
Run Code Online (Sandbox Code Playgroud)

工作完美

只是发布这个答案,这样任何像我这样的菜鸟在外面都可以受益,哈哈