how-to-report-list-of-all-loaded-spring-beans-during-your-spring-mvc-project-startup

Are you working on Enterprise level Spring MVC project? Is it with more than hundreds of classes and packages? Are you really struggling on how to get list of all loaded Spring MVC beans information?

Using below Java code snippet you could get list of loaded Spring Beans information at your fingertip.

Let’s get started:

Step-1

Pre-requisite: Follow How to use AJAX, jQuery in Spring Web MVC (.jsp) – tutorial completely. Make sure it’s working perfectly.

Step-2

Create two new files: CrunchifyMain.java and CrunchifyLoadAllLoadedSpringMVCBean.java

package com.crunchify.controller;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
 * author: Crunchify.com
 * 
 */

public class CrunchifyMain {

        public static void main(String[] args) {
                CrunchifyLoadAllLoadedSpringMVCBean bean = new CrunchifyLoadAllLoadedSpringMVCBean();
                ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "crunchify-bean.xml" });
                bean.crunchifyLogSpringBeans(ctx);
        }
}
package com.crunchify.controller;

import org.springframework.context.ApplicationContext;

/*
 * author: Crunchify.com
 * 
 */

public class CrunchifyLoadAllLoadedSpringMVCBean {

        public void crunchifyLogSpringBeans(ApplicationContext ctx) {
                String[] crunchifyBeans = ctx.getBeanDefinitionNames();
                int loadedTotalBeans = ctx.getBeanDefinitionCount();

                String crunchifyFormat = getCrunchifyFormat(crunchifyBeans);

                printStatement(String.format("------------- Total Loaded Spring Beans: %d ------------- \n", loadedTotalBeans));

                int count = 1;
                for (String crunchifyBean : crunchifyBeans) {
                        Class<?> beanType = ctx.getType(crunchifyBean);
                        Package beanPackage = beanType.getPackage();
                        printStatement(String.format(crunchifyFormat, count++, crunchifyBean, beanType.getName(), beanPackage));
                }
        }

        private static void printStatement(String value) {
                System.out.println(value);
        }

        private static String getCrunchifyFormat(String[] crunchifyBean) {
                int namespace = betterAlignment(crunchifyBean);
                int typespace = (crunchifyBean.length < 100) ? 2 : 3;
                return String.format("%%%dd: Crunchify Bean Name: %%-%ds \n - Bean Type: %%s \n - Package: %%s \n", typespace, namespace);
        }

        private static int betterAlignment(String[] crunchifyBean) {
                int length = 0;

                for (String crunchifyString : crunchifyBean) {
                        if (crunchifyString.length() > length)
                                length = crunchifyString.length();
                }
                return length;
        }
}

Step-3

Create resource folder as Source Folder at the same location as src folder.

Create Source Folder in Eclipse - Crunchify

Step-4

Create config folder as new Source Folder under resource folder. Same as above.

Step-5

Create two new files:

  1. config.properties under config folder
  2. crunchify-bean.xml file under resource folder
TEAM=CRUNCHIFY.COM
FILE_PATH=config/crunchify.properties
ANY_OTHER_KEY_VALUES=AS_PER_YOUR_NEED
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <util:properties id="nodeProperty"
                location="classpath:config/crunchify.properties" />
        <context:property-placeholder
                properties-ref="nodeProperty" />
        <context:component-scan base-package="com.crunchify.controller" />
</beans>

Step-6

Make sure you have below project structure.

print-list-of-all-loaded-spring-beans-during-your-spring-mvc-application-startup

Step-7

Run CrunchifyMain.java program.

You should see result like below in your Eclipse console.

Dec 22, 2014 3:06:28 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5629626a: startup date [Mon Dec 22 15:06:28 CST 2014]; root of context hierarchy
Dec 22, 2014 3:06:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [crunchify-bean.xml]
Dec 22, 2014 3:06:29 PM org.springframework.beans.factory.config.PropertiesFactoryBean loadProperties
INFO: Loading properties file from class path resource [config/crunchify.properties]
------------- Total Loaded Spring Beans: 10 ------------- 

 1: Crunchify Bean Name: nodeProperty                                                                                          
 - Bean Type: java.util.Properties 
 - Package: package java.util, Java Platform API Specification, version 1.7 

 2: Crunchify Bean Name: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0                              
 - Bean Type: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 
 - Package: package org.springframework.beans.factory.config 

 3: Crunchify Bean Name: crunchifyHelloWorld                                                                                   
 - Bean Type: com.crunchify.controller.CrunchifyHelloWorld 
 - Package: package com.crunchify.controller 

 4: Crunchify Bean Name: crunchifySpringAjaxJQuery                                                                             
 - Bean Type: com.crunchify.controller.CrunchifySpringAjaxJQuery 
 - Package: package com.crunchify.controller 

 5: Crunchify Bean Name: org.springframework.context.annotation.internalConfigurationAnnotationProcessor                       
 - Bean Type: org.springframework.context.annotation.ConfigurationClassPostProcessor 
 - Package: package org.springframework.context.annotation 

 6: Crunchify Bean Name: org.springframework.context.annotation.internalAutowiredAnnotationProcessor                           
 - Bean Type: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 
 - Package: package org.springframework.beans.factory.annotation 

 7: Crunchify Bean Name: org.springframework.context.annotation.internalRequiredAnnotationProcessor                            
 - Bean Type: org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor 
 - Package: package org.springframework.beans.factory.annotation 

 8: Crunchify Bean Name: org.springframework.context.annotation.internalCommonAnnotationProcessor                              
 - Bean Type: org.springframework.context.annotation.CommonAnnotationBeanPostProcessor 
 - Package: package org.springframework.context.annotation 

 9: Crunchify Bean Name: org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor           
 - Bean Type: org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor 
 - Package: package org.springframework.context.annotation 

10: Crunchify Bean Name: org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor 
 - Bean Type: org.springframework.context.annotation.ConfigurationClassPostProcessor$EnhancedConfigurationBeanPostProcessor 
 - Package: package org.springframework.context.annotation

There are lot more beans loaded but here I’ve showed only ~10 in above result. Let me know what you see and have any questions.

The post How to Report / Print List of All Loaded Spring Beans during your Spring MVC Application Startup appeared first on Crunchify.