복붙노트

[SPRING] 로드 된 모든 Spring 빈을 출력한다.

SPRING

로드 된 모든 Spring 빈을 출력한다.

시작시로드되는 모든 스프링 빈을 인쇄하는 방법이 있습니까? 스프링 2.0을 사용하고 있습니다.

해결법

  1. ==============================

    1.예. ApplicationContext를 가져 와서 .getBeanDefinitionNames ()를 호출하십시오.

    예. ApplicationContext를 가져 와서 .getBeanDefinitionNames ()를 호출하십시오.

    당신은 컨텍스트를 얻을 수 있습니다 :

    관련 : BeanPostprocessor 빈을 등록하여 각 Bean의 등록을 감지 할 수도 있습니다. 각 Bean에 대해 통지됩니다.

  2. ==============================

    2.

    public class PrintBeans {
        @Autowired
        ApplicationContext applicationContext;
    
        public void printBeans() {
            System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));
        }
    }
    
  3. ==============================

    3.모든 bean 이름과 그 클래스를 출력하십시오 :

    모든 bean 이름과 그 클래스를 출력하십시오 :

    package com.javahash.spring.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloWorldController {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        @RequestMapping("/hello")
        public String hello(@RequestParam(value="key", required=false, defaultValue="World") String name, Model model) {
    
            String[] beanNames = applicationContext.getBeanDefinitionNames();
    
            for (String beanName : beanNames) {
    
                System.out.println(beanName + " : " + applicationContext.getBean(beanName).getClass().toString());
            }
    
            model.addAttribute("name", name);
    
            return "helloworld";
        }
    }
    
  4. ==============================

    4.스프링 부트와 액추에이터 스타터

    스프링 부트와 액추에이터 스타터

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    엔드 포인트 / bean을 점검 할 수 있습니다.

  5. ==============================

    5.너는 부름을 시도 할 수있다.

    너는 부름을 시도 할 수있다.

    org.springframework.beans.factory.ListableBeanFactory.getBeansOfType(Object.class)
    

    또는 org.springframework에 대한 디버그 로깅을 켭니다. (봄 부팅에서는 --logging.level.org.springframework = DEBUG 매개 변수를 사용합니다)

  6. ==============================

    6.Spring-boot-startter-actuator를 사용하면 모든 bean에 쉽게 액세스 할 수 있습니다.

    Spring-boot-startter-actuator를 사용하면 모든 bean에 쉽게 액세스 할 수 있습니다.

    다음은 설정 과정입니다.

    그라데이션 파일에 벨로우즈 추가 :

    compile("org.springframework.boot:spring-boot-starter-actuator")
    

    application.security.enabled = false를 application.property 파일에 추가하십시오.

    다음은 json 파일의 예입니다.

    [{"context":"application:8442","parent":null,"beans":[{"bean":"beanName","aliases":[],"scope":"singleton","type":"packageName$$4b46c703","resource":"null","dependencies":["environment","beanName1","beanName2"]},{"bean":"org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory","aliases":[],"scope":"singleton","type":"org.springframework.core.type.classreading.CachingMetadataReaderFactory","resource":"null","dependencies":[]}]
    

    자세한 정보는 다음 링크를 참조하십시오.

    희망이 당신을 도울 것입니다. 감사 :)

  7. from https://stackoverflow.com/questions/9602664/print-all-the-spring-beans-that-are-loaded by cc-by-sa and MIT license