Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and How To Define An Application Property? - JHIpster

Tags:

jhipster

In Spring Boot, an application property can be defined in application.properties file. For example, a prefix of Rest can be defined as

spring.data.rest.basePath=api

For JHipster which is based on Spring Boot, I guess that an application property could defined in the application.yml file. But none of the follow approach work for me: a 404 error.

spring.data.rest.basePath: api

spring:
    data:
        rest:
            basePath: api

The other possibility is the configuration itself doesn't work.

like image 731
vic Avatar asked Jul 20 '15 23:07

vic


People also ask

Where is application properties located?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure.

How and where can you define properties in Spring Boot?

So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.

How do I create an application property?

Creating application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory. The application. properties file is just a regular text file.

Where is application properties file in Maven project?

properties file in the maven project folder src/main/resources`.


1 Answers

I have same problem and finally figured it out!

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 applicaiton-prod.yml

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;

            public int getMaxIdle() {
                return maxIdle;
            }

            public void setMaxIdle(int maxIdle) {
                this.maxIdle = maxIdle;
            }

            private int maxIdle = 8;
            private int minIdle = 0;


            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();

Hope it helps.

like image 161
Haifeng Zhang Avatar answered Sep 28 '22 04:09

Haifeng Zhang