Repository: jhipster/generator-jhipster
Pull request #8976 (2018-12-21 17:11:41)

Lazy init Spring Beans at start-up.
This is gives a huge performance boost, but is considered experimental, so this is only configured in dev mode for the moment.

See #8954


Patch:
diff --git a/generators/server/files.js b/generators/server/files.js
index 547bdeb764..77c3cc74ee 100644
--- a/generators/server/files.js
+++ b/generators/server/files.js
@@ -795,6 +795,10 @@ const serverFiles = {
                     file: 'package/aop/logging/LoggingAspect.java',
                     renameTo: generator => `${generator.javaDir}aop/logging/LoggingAspect.java`
                 },
+                {
+                    file: 'package/config/lazy/LazyInitBeanFactoryPostProcessor.java',
+                    renameTo: generator => `${generator.javaDir}config/lazy/LazyInitBeanFactoryPostProcessor.java`
+                },
                 {
                     file: 'package/config/DefaultProfileUtil.java',
                     renameTo: generator => `${generator.javaDir}config/DefaultProfileUtil.java`
diff --git a/generators/server/templates/src/main/java/package/config/lazy/LazyInitBeanFactoryPostProcessor.java.ejs b/generators/server/templates/src/main/java/package/config/lazy/LazyInitBeanFactoryPostProcessor.java.ejs
new file mode 100644
index 0000000000..7857de20fe
--- /dev/null
+++ b/generators/server/templates/src/main/java/package/config/lazy/LazyInitBeanFactoryPostProcessor.java.ejs
@@ -0,0 +1,73 @@
+<%#
+ Copyright 2013-2018 the original author or authors from the JHipster project.
+
+ This file is part of the JHipster project, see https://www.jhipster.tech/
+ for more information.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-%>
+package  <%=packageName%>.config.lazy;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.context.annotation.Profile;
+import org.springframework.stereotype.Component;
+
+/**
+ * Lazy initialize all Spring Beans, which boosts up start-up performance.
+ *
+ * This class originally comes from https://github.com/dsyer/spring-boot-allocations where it is written
+ * it can be copied on a project.
+ *
+ * As this is an experimental feature, we only recommend to use it in "dev" mode for the moment.
+ */
+@Component
+@Profile("dev")
+public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
+
+    private Class<?>[] exclusionList;
+
+    public LazyInitBeanFactoryPostProcessor() {
+    }
+
+    public LazyInitBeanFactoryPostProcessor(Class<?>[] exclusionList) {
+        this.exclusionList = exclusionList;
+    }
+
+    @Override
+    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
+        throws BeansException {
+
+        // Iterate over all beans, mark them as lazy if they are not in the exclusion list.
+        for (String beanName : beanFactory.getBeanDefinitionNames()) {
+            if (isLazy(beanName, beanFactory)) {
+                BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
+                definition.setLazyInit(true);
+            }
+        }
+    }
+
+    private boolean isLazy(String beanName, ConfigurableListableBeanFactory beanFactory) {
+        if (exclusionList == null || exclusionList.length == 0) {
+            return true;
+        }
+        for (Class<?> clazz : exclusionList) {
+            if (beanFactory.isTypeMatch(beanName, clazz)) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
diff --git a/generators/server/templates/src/main/resources/logback-spring.xml.ejs b/generators/server/templates/src/main/resources/logback-spring.xml.ejs
index 917c6782f6..5961548bb1 100644
--- a/generators/server/templates/src/main/resources/logback-spring.xml.ejs
+++ b/generators/server/templates/src/main/resources/logback-spring.xml.ejs
@@ -122,6 +122,7 @@
     <logger name="sun.rmi" level="WARN"/><% if (databaseType === 'sql') { %>
     <logger name="liquibase" level="WARN"/>
     <logger name="LiquibaseSchemaResolver" level="INFO"/><% } %>
+    <logger name="springfox.documentation.schema.property" level="ERROR"/>
     <%_ if (applicationType === 'microservice' || applicationType === 'gateway' || cacheProvider === 'hazelcast') { _%>
     <logger name="sun.net.www" level="INFO"/>
     <%_ } _%>


Test patch:
diff --git a/test/utils/expected-files.js b/test/utils/expected-files.js
index 91640882f9..2c6729beb6 100644
--- a/test/utils/expected-files.js
+++ b/test/utils/expected-files.js
@@ -148,6 +148,7 @@ const expectedFiles = {
         `${SERVER_MAIN_RES_DIR}i18n/messages.properties`,
         `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/JhipsterApp.java`,
         `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/aop/logging/LoggingAspect.java`,
+        `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/config/lazy/LazyInitBeanFactoryPostProcessor.java`,
         `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/config/package-info.java`,
         `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/config/ApplicationProperties.java`,
         `${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/config/AsyncConfiguration.java`,