在注册bean中实现 implements ApplicationContextAware
@Component
public class ColorImpl implements ApplicationContextAware {
private ApplicationContext applicationContext;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
============================================================
@Autowired
ColorImpl colorImpl;
public Object testFun1(){
ApplicationContext applicationContext = colorImpl.getApplicationContext();
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
System.out.println("IOC容器注册bean列表");
for(String name : beanDefinitionNames){
System.out.println(name);
}
}