applicationContext.getBeanDefinitionNames()不显示在没有BeanDefinition 实例的情况下注册的 bean 。
package io.velu.core;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class Core {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Core.class);
String[] singletonNames = context.getDefaultListableBeanFactory().getSingletonNames();
for (String singleton : singletonNames) {
System.out.println(singleton);
}
}
}
控制台输出
environment
systemProperties
systemEnvironment
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
messageSource
applicationEventMulticaster
lifecycleProcessor
正如您在输出中看到的,环境、systemProperties、systemEnvironment bean不会使用context.getBeanDefinitionNames()方法显示。
弹簧靴
对于 Spring Boot Web 应用程序,可以使用以下端点列出所有 bean。
@RestController
@RequestMapping("/list")
class ExportController {
@Autowired
private ApplicationContext applicationContext;
@GetMapping("/beans")
@ResponseStatus(value = HttpStatus.OK)
String[] registeredBeans() {
return printBeans();
}
private String[] printBeans() {
AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
if (autowireCapableBeanFactory instanceof SingletonBeanRegistry) {
String[] singletonNames = ((SingletonBeanRegistry) autowireCapableBeanFactory).getSingletonNames();
for (String singleton : singletonNames) {
System.out.println(singleton);
}
return singletonNames;
}
return null;
}
}
[“autoConfigurationReport”、“springApplicationArguments”、“springBootBanner”、“springBootLoggingSystem”、“环境”、“systemProperties”、“systemEnvironment”、“org.springframework.context.annotation.internalConfigurationAnnotationProcessor”、“org.springframework.boot.autoconfigure. internalCachingMetadataReaderFactory”、“org.springframework.boot.autoconfigure.condition.BeanTypeRegistry”、“org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry”、“propertySourcesPlaceholderConfigurer”、“org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store” , "preserveErrorControllerTargetClassPostProcessor”、“org.springframework.context.annotation.internalAutowiredAnnotationProcessor”、“org.springframework.context.annotation.internalRequiredAnnotationProcessor”、“org.springframework.context.annotation.internalCommonAnnotationProcessor”、“org.springframework.boot.context.properties. ConfigurationPropertiesBindingPostProcessor”、“org.springframework.scheduling.annotation.ProxyAsyncConfiguration”、“org.springframework.context.annotation.internalAsyncAnnotationProcessor”、“methodValidationPostProcessor”、“embeddedServletContainerCustomizerBeanPostProcessor”、“errorPageRegistrarBeanPostProcessor”、“messageSource”、“applicationEventMulticaster”、“org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat”、“tomcatEmbeddedServletContainerFactory”、“org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration$TomcatWebSocketConfiguration”、“websocketContainerCustomizer”、“spring.http.encoding- org.springframework.boot.autoconfigure.web.HttpEncodingProperties”、“org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration”、“localeCharsetMappingsCustomizer”、“org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration”、“serverProperties”、“重复服务器属性检测器”、“spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties”、“org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration”、“conventionErrorViewResolver”、“org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration”、“errorPageCustomizer”、“servletContext”、“ contextParameters”、“contextAttributes”、“spring.mvc-org.springframework.boot.autoconfigure.web.WebMvcProperties”、“spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties”、“org.springframework. boot.autoconfigure.web.MultipartAutoConfiguration”、“multipartConfigElement”、“org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration”、“org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration$DispatcherServletConfiguration”、“dispatcherServlet”、“dispatcherServletRegistration”、“requestContextFilter”、“org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration”、“hiddenHttpMethodFilter” 、“httpPutFormContentFilter”、“characterEncodingFilter”、“org.springframework.context.event.internalEventListenerProcessor”、“org.springframework.context.event.internalEventListenerFactory”、“reportGeneratorApplication”、“exportController”、“exportService”、“org.springframework.开机。autoconfigure.AutoConfigurationPackages”、“org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration”、“org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration”、“spring.jackson-org.springframework.boot.autoconfigure.jackson. JacksonProperties”、“standardJacksonObjectMapperBuilderCustomizer”、“org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration”、“org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration”、“jsonComponentModule”、“jacksonObjectMapperBuilder”、“org.springframework.引导自动配置杰克逊。JacksonAutoConfiguration$JacksonObjectMapperConfiguration”、“jacksonObjectMapper”、“org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration”、“org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration”、“org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration” , “org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration”, “defaultValidator”, “org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration”, “error”, “beanNameViewResolver”, “errorAttributes”, “basicErrorController” , "org.springframework.boot.autoconfigure.web。WebMvcAutoConfiguration$EnableWebMvcConfiguration”、“org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter”、“mvcContentNegotiationManager”、“org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration”、“stringHttpMessageConverter”、“org.springframework. boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration”、“mappingJackson2HttpMessageConverter”、“org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration”、“messageConverters”、“mvcConversionService”、“mvcValidator”、“requestMappingHandlerAdapter”、“mvcResourceUrlProvider”、“requestMappingHandlerMapping”、“mvcPathMatcher”、“mvcUrlPathHelper”、“viewControllerHandlerMapping”、“beanNameHandlerMapping”、“resourceHandlerMapping”、“defaultServletHandlerMapping”、“mvcUriComponentsContributor”、“httpRequestHandlerAdapter”、“simpleControllerHandlerAdapter”、“handlerExceptionResolver” ,“mvcViewResolver”,“org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration”,“faviconRequestHandler”,“faviconHandlerMapping”,“defaultViewResolver”,“viewResolver”、“welcomePageHandlerMapping”、“org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration”、“objectNamingStrategy”、“mbeanServer”、“mbeanExporter”、“org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration”、“springApplicationAdminRegistrar” ,“org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration”,“org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration”,“spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties”,“org. springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration”、“multipartResolver”、“org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration$RestTemplateConfiguration”、“restTemplateBuilder”、“org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration”、“spring.devtools-org.springframework.boot.devtools.autoconfigure.DevToolsProperties”、“ org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration$RestartConfiguration”、“fileSystemWatcherFactory”、“classPathRestartStrategy”、“classPathFileSystemWatcher”、“hateoasObjenesisCacheDisabler”、“org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration$LiveReloadConfiguration$LiveReloadServerConfiguration”、“ org.springframework.boot.devtools.autoconfigure。LocalDevToolsAutoConfiguration$LiveReloadConfiguration", "optionalLiveReloadServer", "org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration", "lifecycleProcessor"]