首页  

SpringIoC容器要点     所属分类 spring 浏览量 973
BeanDefinition
bean 对象的 class类型 是否是抽象类 构造方法和参数 其它属性等

BeanDefinitionRegistry
BeanFactory

DefaultListableBeanFactory

BeanFactory 接口 主要方法
getBean containBean getType getAliases 

BeanDefinitionRegistry 接口主要方法 
registerBeanDefinition removeBeanDefinition getBeanDefinition 

BeanFactory         内部使用
ApplicationContext  面向应用


BeanFactory 手工注册 和 从xml加载bean定义 实例



    public static void main(String[] args) throws Exception {
        
        DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
        AbstractBeanDefinition definition = new RootBeanDefinition(Pet.class);
        MutablePropertyValues pvs = definition.getPropertyValues();
        System.out.println(pvs.size());
        System.out.println(pvs);
        pvs.addPropertyValue("id", "1");
        pvs.addPropertyValue("name", "tiger");

        System.out.println(pvs.size());
        System.out.println(pvs);
        
        beanRegistry.registerBeanDefinition("pet",definition);
        BeanFactory container = (BeanFactory)beanRegistry;
        Pet pet = (Pet)container.getBean("pet");
        System.out.println(pet);
        
        //
        loadFromXml();

    }
    
    private static void loadFromXml() {
        DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory(); 
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanRegistry);
        beanDefinitionReader.loadBeanDefinitions("classpath:simple.xml");
        BeanFactory container = (BeanFactory)beanRegistry;
        Pet pet = (Pet)container.getBean("pet");
        System.out.println(pet);
    }


完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/java/spring/BeanFactoryTest.java


IoC容器负责管理 bean生命周期 扩展点 BeanFactoryPostProcessor 在容器实例化对象之前修改bean定义 org.springframework.core.Ordered public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; } PropertyPlaceholderConfigurer 配置集中管理 使用配置值 替换 BeanDefinition 中的占位符 BeanPostProcessor BeanFactoryPostProcessor处理bean的定义 BeanPostProcessor处理实例化之后的bean public interface BeanPostProcessor { // 前置处理 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; // 后置处理 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; } ApplicationContextAware ApplicationContextAwareProcessor implements BeanPostProcessor postProcessBeforeInitialization invokeAwareInterfaces private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
spring bean 生命周期实例 spring bean生命周期及扩展点 BeanFactory和ApplicationContext的区别 Spring的BeanFactoryPostProcessor和BeanPostProcessor

上一篇     下一篇
AspectJ简介及实例

Jackson JSON的三种处理方式

Springboot监控端点Actuator实例

JavaConfig与常见的Annotation

spring容器事件机制

SpringFactoriesLoader 简介