Getting Started 06 - Bean Defining Drop Advanced Read
InputStream BeanFactory acceptable constructor as an argument, or may be implemented to accept the object as a parameter org.springframework.core.io.Resource interface, such ClassPathResource, FileSystemResource, InputStreamResource, ServletContextResource, UrlResource, may be used on different occasions the corresponding Class, ClassPathResource as an Example:
Resource resource = new classpathResource ("bean.xml");
Beanfactory factory = new xmlbeanfactory (resource);
Hellobean Hello = (Hellobean) Factory.getBean ("Hellobean");
....
In the bean definition file, if there is multiple beans definitions, you want to retrieve all the beans at a time, you can use org.springframework.beans.factory.listableBeanFactory, retrieve all corresponding beans by their corresponding method Objects, for example, if the contents of the bean definition file are as follows:
XML Version = "1.0" encoding = "UTF-8"?>
bean>
bean>
beans>
Then we can retrieve all Bean objects again according to the type, for example:
Resource resource = new classpathResource ("bean.xml");
ListableBeanFactory Factory = New XmlbeanFactory (Resource);
Map Hellobeans = Factory.getBeansoftype (HelloBean.class, False, False);
You can see how the API file understands getBeansOfType () or other ListableBeanFactory methods. Write all bean definitions on a bean definition file only on small programs, for large applications, the required bean is very large, we can't write it in the same definition file, The previous method binds a defined file to a beanfactory, and the creation of BeanFactory requires resource, which is obviously not feasible. We can use org.springframework.beans.factory.xml.xmlbeandefinitionReader to make multiple defined files, it requires an object of the actual org.springframework.beans.factory.support.BeandefinitionRegistry as a parameter, such as ORG .springframework.beans.factory.support.defaultListableBeanFactory, XmlBeandeFinitionReader uses a beandefinitionRegistry type object to register multiple bean definition files, we don't have to create a beanfactory for each bean defined file. A practical example of using: beandefinitionregistry reg = new defaultListableBeanFactory ();
XmlbeandefinitionReader Reader = New XmlbeandefinitionReader (REG);
// Load the bean definition file
Reader.LoadBeandefinitions (New ClassPathResource ("Bean1.xml");
Reader.LoadBeandefinitions (New ClassPathResource ("Bean2.xml");
....
// get bean
Beanfactory bf = (beanfactory) REG;
Object o = bf.getBean ("Hellobean");
....
DEFAULTLISTABLEBEANFACTORY simultaneously on interfaces such as beandefinitionregistry, beanfactory, ListableBeanFactory, in order to use BeanFactory features, we must convert the interface, as shown above. Temporarily introduce this side, there is any defined file reading method worth noting, it will be added here.