IOC底层原理-bean初始化过程解析

 

 

package com.itheima.config.postprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactory implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("bean的工厂制作好了,还有什么事情需要处理");
    }
}

package com.itheima.config.postprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBean implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("bean之前巴拉巴拉");
        System.out.println(beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("bean之后巴拉巴拉");

        return bean;
    }
}
package com.itheima.config;

import com.itheima.config.filter.MyTypeFilter;
import com.itheima.config.postprocessor.MyBean;
import com.itheima.config.postprocessor.MyBeanFactory;
import com.itheima.config.registrar.CustomeImportBeanDefinitionRegistrar;
import com.itheima.config.registrar.MyImportBeanDefinitionRegistrar;
import com.itheima.config.selector.CustomerImportSelector;
import com.itheima.config.selector.MyImportSelector;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Configuration
@ComponentScan("com.itheima")
@Import({MyBeanFactory.class, CustomeImportBeanDefinitionRegistrar.class, MyBean.class})
//@Import(CustomerImportSelector.class)
/*@ComponentScan(
        value = "com.itheima",
        excludeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                classes = Service.class
        )
)*/
/*@ComponentScan(
        value = "com.itheima",
        excludeFilters = @ComponentScan.Filter(
                type = FilterType.CUSTOM,
                classes = MyTypeFilter.class
        )
)*/
public class SpringConfig {

}
package com.itheima.dao.impl;

import com.itheima.dao.EquipmentDao;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class EquipmentDaoImpl implements EquipmentDao,InitializingBean {

    public void save() {
        System.out.println("equipment dao running...");
    }

    @Override
    //定义当前bean初始化操作,功效等同于init-method属性配置
    public void afterPropertiesSet() throws Exception {
        SqlSessionFactoryBean fb;
        System.out.println("EquipmentDaoImpl......bean ...init......");
    }
}
package com.itheima.dao.impl;

import org.springframework.beans.factory.FactoryBean;

public class EquipmentDaoImplFactoryBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        return new EquipmentDaoImpl();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值