项目准备:
环境:
java 8
gradle 4.10
代码:
build.gradle :
plugins {
id 'java'
}
group 'cn.fancy.spring.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.springframework/spring-core
compile group: 'org.springframework', name: 'spring-core', version: '5.1.2.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-beans
compile group: 'org.springframework', name: 'spring-beans', version: '5.1.2.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
MyTestBean
package cn.fancy.testbean;
/**
* @author fancy
* @date 2018-11-23 08:48
*/
public class MyTestBean {
private String name = "fancy";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
beanFactoryTest.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.xsd " >
<bean id="myTestBean" class="cn.fancy.testbean.MyTestBean">
<property name="name" value="张三"/>
</bean>
</beans>
MyBeanFactoryTest
package cn.fancy.testbean;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.management.loading.ClassLoaderRepository;
/**
* @author fancy
* @date 2018-11-23 08:49
*/
public class MyBeanFactoryTest {
@Test
public void testSimpleLoad () {
XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
MyTestBean myTestBean = (MyTestBean)xmlBeanFactory.getBean("myTestBean");
System.out.println(myTestBean.getName());
}
}
我们发现XmlBeanFactory过时了,那我们不使用XmlBeanFactory,怎么去读取配置生成bean呢?
我们先看下下面这行代码XmlBeanFactory 做了什么工作?
/**
* Create a new XmlBeanFactory with the given resource,
* which must be parsable using DOM.
* @param resource the XML resource to load bean definitions from
* @throws BeansException in case of loading or parsing errors
*/
public XmlBeanFactory(Resource resource) throws BeansException {
this(resource, null);
}
/**
* Create a new XmlBeanFactory with the given input stream,
* which must be parsable using DOM.
* @param resource the XML resource to load bean definitions from
* @param parentBeanFactory parent bean factory
* @throws BeansException in case of loading or parsing errors
*/
public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
super(parentBeanFactory);
this.reader.loadBeanDefinitions(resource);
}
我们在调用new XmlBeanFactory(new ClassPathResource(“beanFactoryTest.xml”))的时候,主要讲resource 资源传入this.reader.loadBeanDefinitions(resource);
那么this.reader是什么呢?
先看一下以下代码:
public class XmlBeanFactory extends DefaultListableBeanFactory {
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
...
}
XmlBeanDefinitionReader 的构造器
/**
* Create new XmlBeanDefinitionReader for the given bean factory.
* @param registry the BeanFactory to load bean definitions into,
* in the form of a BeanDefinitionRegistry
*/
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
}
根据以上,我们可以仿写一个XmlBeanFactory读取xml的逻辑
代码如下:
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinitionReader beanDefinitionReader=new XmlBeanDefinitionReader(factory);
beanDefinitionReader.loadBeanDefinitions(new ClassPathResource("beanFactoryTest.xml"));
MyTestBean myTestBean = (MyTestBean)factory.getBean("myTestBean");
System.out.println(myTestBean.getName());
根据代码上面的代码,我们能看出XmlBeanFactory继承DefaultListableBeanFactory,同时只比DefaultListableBeanFactory多实现了this.reader.loadBeanDefinitions(resource);这个方法.
那么这个方法干了件什么事情呢?下节在讨论!