Spring Getting Started 06 - Bean Defines the Advanced Access Reading

xiaoxiao2021-03-06  40

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:

Hello! Justin!

Hello! Caterpillar!

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.

转载请注明原文地址:https://www.9cbs.com/read-60924.html

New Post(0)