@Component @Scope(scopeName="singleton") public class SpringContextHolder extends Object
可以使用 Import 注入Bean例子:
@Import(SpringContextHolder.class)
@Configuration
public class Config {
}
| Constructor and Description |
|---|
SpringContextHolder() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsBean(String name)
Does this bean factory contain a bean definition or externally registered
singleton instance with the given name?
|
static boolean |
containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given
name.
|
static boolean |
containsLocalBean(String name)
Return whether the local bean factory contains a bean of the given name,
ignoring beans defined in ancestor contexts.
|
static org.springframework.context.ApplicationContext |
getApplicationContext()
取得存储在静态变量中的ApplicationContext.
|
static <T> T |
getBean(Class<T> requiredType)
从Spring applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
static <T> T |
getBean(String name)
从Spring applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
static <T> T |
getBean(String name,
Class<T> requiredType)
从Spring applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
static void |
setApplicationContext(org.springframework.boot.context.event.ApplicationReadyEvent event)
使用
ApplicationReadyEvent 事件获取 ApplicationContext 注入到静态变量中 |
@EventListener public static void setApplicationContext(org.springframework.boot.context.event.ApplicationReadyEvent event)
ApplicationReadyEvent 事件获取 ApplicationContext 注入到静态变量中event - ApplicationReadyEventpublic static org.springframework.context.ApplicationContext getApplicationContext()
public static <T> T getBean(String name)
T - bean class typename - bean namepublic static <T> T getBean(String name, Class<T> requiredType)
T - bean class typename - bean namerequiredType - type the bean must matchpublic static <T> T getBean(Class<T> requiredType)
T - bean class typerequiredType - type the bean must matchpublic static boolean containsBean(String name)
If the given name is an alias, it will be translated back to the corresponding canonical bean name.
If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance.
If a bean definition or singleton instance matching the given name is
found, this method will return true whether the named bean
definition is concrete or abstract, lazy or eager, in scope or not.
Therefore, note that a true return value from this method does
not necessarily indicate that getBean(java.lang.String) will be able to obtain an
instance for the same name.
name - the name of the bean to querypublic static boolean containsBeanDefinition(String beanName)
Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
beanName - the name of the bean to look forcontainsBean(java.lang.String)public static boolean containsLocalBean(String name)
This is an alternative to containsBean, ignoring a bean of the
given name from an ancestor bean factory.
name - the name of the bean to queryBeanFactory.containsBean(java.lang.String)Copyright © 2018–2020. All rights reserved.