Spring IOC实现原理笔记(一)

基本用法

建一个测试的bean如下:

public class MyTestBean {
    private String testStr;

    public String getTestStr() {
        return testStr;
    }

    public void setTestStr(String testStr) {
        this.testStr = testStr;
    }

    public void test(){
        System.out.println(testStr);
    }
}

beans.xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

   <bean id="myTestBean" class="com.season.MyTestBean">
       <property name="testStr" value="hello world"/>
   </bean>
</beans>

代码结构如下
code_preview_1

测试代码如下:执行后会看到输出”hello world”

@Test
public void testSimpleLoad() {
    ApplicationContext actx = new ClassPathXmlApplicationContext("beans.xml");
    MyTestBean bean = (MyTestBean) actx.getBean("myTestBean");
    bean.test();
}

ClassPathXmlApplicationContext用于加载类路径(classpath:)下的文件,而FileSystemXmlApplicationContext用于加载文件系统路径(file:)的文件,它们都可以显式使用带资源类型前缀的路径,区别在于如果不显式指定资源前缀,则分别将路径解析为类路径和文件系统路径

来看看ClassPathXmlApplicationContext的继承关系

ClassPathXmlApplicationContext

BeanFactory接口位于类结构树的顶端,我们经常用到的就是getBean方法,来从spring容器中获得想要的对象。

BeanFactory

BeanFactory接口通过其他接口得到不断扩展,其中ApplicationContext就是有它派生而来。ApplicationContext提供了更多面向实际应用的功能,都是可以通过配置来实现的。上面提到的ClassPathXmlApplicationContext和FileSystemXmlApplicationContext是它的两个主要实现类。

BeanNameAware,InitializingBean,BeanFactoryAware等

让MyTestBean实现一些接口

public class MyTestBean implements BeanNameAware,InitializingBean,BeanFactoryAware{
...
       @Override
       public void setBeanName(String beanName) {
           System.out.println("MyTestBean - setBeanName... beanName: "+beanName);
       }
       @Override
       public void afterPropertiesSet() throws Exception {
           System.out.println("MyTestBean - afterPropertiesSet...");
       }
       @Override
       public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
           System.out.println("MyTestBean - setBeanFactory... beanFactory:"+beanFactory);
       }
}

输出如下:

MyTestBean - setBeanName... beanName: myTestBean
MyTestBean - setBeanFactory... beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@1213b92: defining beans [myTestBean]; root of factory hierarchy
MyTestBean - afterPropertiesSet...
hello world

BeanFactoryPostProcessor用法

建一个类实现BeanFactoryPostProcessor接口

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanFactoryPostProcessor - postProcessBeanFactory...");
        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
            MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
            if (propertyValues.contains("testStr")) {
                propertyValues.addPropertyValue("testStr", "MyBeanFactoryPostProcessor改变了属性");
            }
        }
    }
}

在beans.xml中加上改bean

<bean class="com.season.MyBeanFactoryPostProcessor"/>

运行输出结果如下:

MyBeanFactoryPostProcessor - postProcessBeanFactory...
MyTestBean - setBeanName... beanName: myTestBean
MyTestBean - setBeanFactory... beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@127bd23: defining beans [myTestBean,com.season.MyBeanFactoryPostProcessor#0]; root of factory hierarchy
MyTestBean - afterPropertiesSet...
MyBeanFactoryPostProcessor改变了属性

BeanPostProcessor用法

建一个类实现BeanPostProcessor接口

public class MyBeanPostProcessor implements BeanPostProcessor {
     @Override
     public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
         System.out.println("MyBeanPostProcessor - postProcessBeforeInitialization...bean: "+o+"  beanName: "+s);
         return o;
     }
     @Override
     public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
         System.out.println("MyBeanPostProcessor - postProcessAfterInitialization...");
         return o;
     }
}

在beans.xml中加上改bean

<bean class="com.season.MyBeanPostProcessor"/>

输出结果如下:

MyBeanFactoryPostProcessor - postProcessBeanFactory...
MyTestBean - setBeanName... beanName: myTestBean
MyTestBean - setBeanFactory... beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@127bd23: defining beans [myTestBean,com.season.MyBeanPostProcessor#0,com.season.MyBeanFactoryPostProcessor#0]; root of factory hierarchy
MyBeanPostProcessor - postProcessBeforeInitialization...bean: com.season.MyTestBean@153b3e6  beanName: myTestBean
MyTestBean - afterPropertiesSet...
MyBeanPostProcessor - postProcessAfterInitialization...
MyBeanFactoryPostProcessor改变了属性

下篇文章开始分析源码,对上面的输出结果进行探究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值