Spring MVC 애플리케이션 시작 동안 로드된 모든 Spring Beans의 목록을 보고/인쇄하는 방법
게시 됨: 2016-10-24
엔터프라이즈 수준의 Spring MVC 프로젝트에서 작업하고 있습니까? 수백 개 이상의 클래스와 패키지가 있습니까? 로드된 모든 Spring MVC 빈 정보 목록을 가져오는 방법에 대해 정말 고심하고 있습니까?
아래 Java 코드 스니펫을 사용하면 로드된 Spring Beans 정보 목록을 손쉽게 얻을 수 있습니다.
시작하자:
1 단계
전제 조건: Spring Web MVC(.jsp)에서 AJAX, jQuery를 사용하는 방법 – 자습서를 완전히 따르십시오. 완벽하게 작동하는지 확인하십시오.
2 단계
two new
파일 생성: CrunchifyMain.java
및 CrunchifyLoadAllLoadedSpringMVCBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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 ) ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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 ; } } |
3단계
src
폴더와 같은 위치에 Source Folder
로 resource
폴더를 생성합니다.

4단계
resource
폴더 아래에 새로운 Source Folder
로 config
폴더를 생성합니다. 같은 상기와.
5단계
두 개의 새 파일을 만듭니다.
-
config
폴더 아래의config.properties
-
resource
폴더 아래crunchify-bean.xml
파일
1 2 3 |
TEAM = CRUNCHIFY . COM FILE_PATH = config / crunchify . properties ANY_OTHER_KEY_VALUES = AS_PER_YOUR_NEED |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<? 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 > |
6단계
아래 프로젝트 구조가 있는지 확인하십시오.


7단계
CrunchifyMain.java
프로그램을 실행합니다.
Eclipse 콘솔에 아래와 같은 결과가 표시되어야 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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 |
더 많은 빈이 로드되어 있지만 여기서는 위의 결과에서 ~10개만 보여주었습니다. 무엇을 보고 질문이 있는지 알려주십시오.