基本用法
建一个测试的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>
代码结构如下
测试代码如下:执行后会看到输出”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的继承关系
BeanFactory接口位于类结构树的顶端,我们经常用到的就是getBean方法,来从spring容器中获得想要的对象。
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改变了属性
下篇文章开始分析源码,对上面的输出结果进行探究。