ApplicationContextAware 理解
我们常用的IOC 容器是 ApplicationContext, 她的顶层接口是 BeanFactory, ApplicationContext 对BeanFactory 进行了扩展。
package org.springframework.context;
import org.springframework.beans.factory.HierarchicalBeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.lang.Nullable; public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver { @Nullable String getId(); String getApplicationName(); String getDisplayName(); long getStartupDate(); @Nullable ApplicationContext getParent(); AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException; }
其中 ListableBeanFactory、HierarchicalBeanFactory 分别继承自 BeanFactory, BeanFactory 内容如下,记录下来,方便多看,多熟悉
package org.springframework.beans.factory;
import org.springframework.beans.BeansException; import org.springframework.core.ResolvableType; import org.springframework.lang.Nullable; public interface BeanFactory { String FACTORY_BEAN_PREFIX = "&"; Object getBean(String var1) throws BeansException; <T> T getBean(String var1, Class<T> var2) throws BeansException; Object getBean(String var1, Object... var2) throws BeansException; <T> T getBean(Class<T> var1) throws BeansException; <T> T getBean(Class<T> var1, Object... var2) throws BeansException; <T> ObjectProvider<T> getBeanProvider(Class<T> var1); <T> ObjectProvider<T> getBeanProvider(ResolvableType var1); boolean containsBean(String var1); boolean isSingleton(String var1) throws NoSuchBeanDefinitionException; boolean isPrototype(String var1) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException; @Nullable Class<?> getType(String var1) throws NoSuchBeanDefinitionException; String[] getAliases(String var1); }
我们拿到 IOC 容器的方式有3种,使用ApplicationContext接口下的三个实现类:ClassPathXmlApplicationContext、FileSystemXmlApplicationContext、AnnotationConfigApplicationContext, 但是 SpringBoot 中,IOC 配置文件都被简化了,无法通过上述3种方式拿到 IOC 容器,但是有时候需求又必须是用 Spring 容器才能实现,所以:
ApplicationContextAware 就是用来获取框架自动初始化到IOC 容器对象的。
例:
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContext方法 * @param applicationContext */ @Override public void setApplicationContext(ApplicationContext applicationContext) { if(SpringContextUtil.applicationContext == null) { SpringContextUtil.applicationContext = applicationContext; } } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通过class获取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
使用IOC 容器对象
static RiskBlo riskBlo ; @PostConstruct public void init() { riskBlo = SpringContextUtil.getBean(RiskBlo.class); }
些许混乱,边看边记录, 后面整理在吸收