1. 程式人生 > >獲取所有bean的名字

獲取所有bean的名字

nbsp log str cati cat autowired strings tap 所有

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;



WebApplicationContext ac = ContextLoaderListener.getCurrentWebApplicationContext(); //ApplicationContext ac = SpringContext.getApplicationContext(); String[] strings=ac.getBeanDefinitionNames(); System.out.println(strings.length); AutowireCapableBeanFactory beanFactory
= ac.getAutowireCapableBeanFactory(); beanFactory.autowireBean(a); String name = a.getClass().getName(); beanFactory.initializeBean(a, name); strings=ac.getBeanDefinitionNames(); System.out.println(strings.length); for (String string : strings) { System.out.println(string); }

獲取所有bean的名字