Aware接口
ApplicationContextAware
通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
ApplicationEventPublisherAware
设置事件事件的发布者,传入ApplicationEventPublisher 。
public interface ApplicationEventPublisherAware extends Aware {
void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);
}
BeanClassLoaderAware
回调类加载器,传入ClassLoader 。
public interface BeanClassLoaderAware extends Aware {
void setBeanClassLoader(ClassLoader classLoader);
}
BeanFactoryAware
每次创建当前Bean的时候会回调,传入BeanFactory 。
public interface BeanFactoryAware extends Aware {
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
BeanNameAware
每次创建bean的时候都会回调方法,传入当前bean的name。
public interface BeanNameAware extends Aware {
void setBeanName(String name);
}
EmbeddedValueResolverAware
传入字符串解析实例resolver
public interface EmbeddedValueResolverAware extends Aware {
void setEmbeddedValueResolver(StringValueResolver resolver);
}
EnvironmentAware
获取环境实例对象,用于获取环境参数值。
public interface EnvironmentAware extends Aware {
void setEnvironment(Environment environment);
}
ImportAware
获取AnnotationMetadata 注释元信息
public interface ImportAware extends Aware {
void setImportMetadata(AnnotationMetadata importMetadata);
}
LoadTimeWeaverAware
public interface LoadTimeWeaverAware extends Aware {
void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver);
}
Aware使用例子:
public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("传入的ioc:"+applicationContext);
this.applicationContext = applicationContext;
}
public void setBeanName(String name) {
System.out.println("当前bean的名字:"+name);
}
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String resolveStringValue = resolver.resolveStringValue("你好 ${os.name} 我是 #{20*18}");
System.out.println("解析的字符串:"+resolveStringValue);
}
}
ImportSelector
返回需要导入的组件的全类名数组;
public class MyImportSelector implements ImportSelector {
//返回值,就是到导入到容器中的组件全类名
//AnnotationMetadata:当前标注@Import注解的类的所有注解信息
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//importingClassMetadata
return new String[]{"com.spring.bean.Blue","com.spring.bean.Yellow"};
}
}
ImportBeanDefinitionRegistrar
手动注册bean到容器中
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* AnnotationMetadata:当前类的注解信息
* BeanDefinitionRegistry:BeanDefinition注册类;
* 把所有需要添加到容器中的bean;调用
* BeanDefinitionRegistry.registerBeanDefinition手工注册进来
*/
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean definition = registry.containsBeanDefinition("com.spring.bean.Red");
boolean definition2 = registry.containsBeanDefinition("com.spring.bean.Blue");
if(definition && definition2){
//指定Bean定义信息;(Bean的类型,Bean。。。)
RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
//注册一个Bean,指定bean名
registry.registerBeanDefinition("rainBow", beanDefinition);
}
}
}
FactoryBean
Spring提供的 FactoryBean(工厂Bean);
1)、默认获取到的是工厂bean调用getObject创建的对象
2)、要获取工厂Bean本身,我们需要给id前面加一个&
例:ColorFactoryBean factory= applicationContext.getBean("colorFactoryBean");
Color bean = applicationContext.getBean("&colorFactoryBean");
//创建一个Spring定义的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
//返回一个Color对象,这个对象会添加到容器中
public Color getObject() throws Exception {
return new Color();
}
public Class<?> getObjectType() {
return Color.class;
}
//true:这个bean是单实例,在容器中保存一份
//false:多实例,每次获取都会创建一个新的bean;
public boolean isSingleton() {
return false;
}
}
BeanPostProcessor
后置处理器:初始化前后进行处理工作,将后置处理器加入到容器中。
public class MyBeanPostProcessor implements BeanPostProcessor {
//初始化之前
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//初始化之后
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
InstantiationAwareBeanPostProcessor
初始化对象后置处理器。
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
//bean初始化之前
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
//bean初始化之后
boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
//bean属性设置
PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException;
}
SmartInstantiationAwareBeanPostProcessor
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
/** 预测Bean的类型,返回第一个预测成功的Class类型,如果不能预测返回null */
Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException;
/**
* 选择合适的构造器,比如目标对象有多个构造器,在这里可以进行一些定制化,选择合适的构造器
* beanClass参数表示目标实例的类型,beanName是目标实例在Spring容器中的name
* 返回值是个构造器数组,如果返回null,会执行下一个PostProcessor的determineCandidateConstructors方法;
* 否则选取该PostProcessor选择的构造器
*/
Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException;
/**
* 获得提前暴露的bean引用。主要用于解决循环引用的问题 只有单 * 例对象才会调用此方法
*/
Object getEarlyBeanReference(Object bean, String beanName) throws BeansException;
}
TypeFilter
扫描组件过滤器
public class MyTypeFilter implements TypeFilter {
/**
* metadataReader:读取到的当前正在扫描的类的信息
* metadataReaderFactory:可以获取到其他任何类信息的
*/
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
//获取当前类注解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前正在扫描的类的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前类资源(类的路径)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
if(className.contains("er")){
return true;
}
return false;
}
}
@Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class}
BeanFactoryPostProcessor
BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。
public interface BeanFactoryPostProcessor {
//在容器实例化任何bean之前调用。
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor可以获得BeanDefinitionRegistry 对象,通过它可以动态的注册组件。
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
//可以动态的注册组件
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
例子:
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor...bean的数量:"+beanFactory.getBeanDefinitionCount());
}
//BeanDefinitionRegistry Bean定义信息的保存中心,以后BeanFactory就是按照BeanDefinitionRegistry里面保存的每一个bean定义信息创建bean实例;
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("postProcessBeanDefinitionRegistry...bean的数量:"+registry.getBeanDefinitionCount());
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
registry.registerBeanDefinition("hello", beanDefinition);
}
}
ApplicationListener
事件监听器
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
//当容器中发布此事件以后,方法触发
void onApplicationEvent(E event);
}
相关例子可以在spring注解开发(一)中查看