Beanfactory and factorybean of spring

Source: Internet
Author: User
org.springframework.beansAnd org.springframework.contextPackages are the basis of Spring IoC containers. BeanFactoryThe Advanced Configuration mechanism makes it possible to manage objects of any nature. ApplicationContextYes BeanFactoryFunctions are further enhanced, such as easier integration with Spring AOP, message resource processing (International processing), event transfer, and context implementation of different application layers (such as for Web Applications WebApplicationContext). In short, BeanFactoryThe preparation framework and basic functions are provided. ApplicationContextThen, more functions are added to support the core content of the enterprise. ApplicationContextCompletely BeanFactoryTherefore BeanFactoryThe capabilities and behaviors are also applicable ApplicationContext. org.springframework.beans.factory.BeanFactoryIs Spring IoC ContainerThe IOC container is responsible for accommodating the previously described bean and managing the bean. In spring, BeanFactoryIs the core interface of the IOC container. Its responsibilities include instantiating, locating, configuring objects in the application, and establishing dependencies between these objects. Spring provides us with a lot of easy-to-use BeanFactoryImplementation, XmlBeanFactoryIs the most commonly used one. This implementation describes the objects that constitute the application and the dependencies between objects in XML format. XmlBeanFactoryClass will hold this XML Configure metadataAnd use it to build a fully configurable system or application. The instantiation of the Spring IoC container is very simple, as shown in the following example:
Resource resource = new FileSystemResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);
... Or...
ClassPathResource resource = new ClassPathResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);
... Or...
ApplicationContext context = new ClassPathXmlApplicationContext(        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});// of course, an ApplicationContext is just a BeanFactoryBeanFactory factory = (BeanFactory) context;
3.2.2.1. It is very useful to split the xml configuration file into multiple parts based on XML configuration metadata. To load multiple XML files to generate an applicationcontext instance, you can use the file path as a string array to send it to the applicationcontext constructor. Bean Factory reads bean definitions from multiple files by calling bean defintion reader. Usually, the spring team tends to do this, because the configuration does not find their combination with other configuration files. Another method is to use one or more <import/>Element to load bean definitions from one or more files. All <import/>The element must be placed in <bean/>To complete bean-defined import. Let's take an example:
<beans><import resource="services.xml"/>    <import resource="resources/messageSource.xml"/>    <import resource="/resources/themeSource.xml"/>      <bean id="bean1" class="..."/>    <bean id="bean2" class="..."/>  </beans>
In the above example, we start from three external files: services.xml, messageSource.xmlAnd themeSource.xmlTo load bean definitions. All relative paths are used here. Therefore, in this example services.xmlMust be in the same directory or class path as the import file, and messageSource.xmlAnd themeSource.xmlMust be placed in the directory where the import file is located resourcesDirectory. As you can see, the slash '/' At the beginning can be ignored. Therefore, it may be better to skip the slash. According to the schema (or DTD) of the spring xml configuration file, the imported file must be a fully valid XML bean definition file and the root node must be <beans/>Element. When XML is used to describe the configuration metadata <bean/>Element classAttribute to specify the type of the instantiated object. classAttribute (corresponding BeanDefinitionInstance ClassAttribute) is usually required (but there are two exceptions, "instantiate using the instance factory method" and "bean-defined inheritance "). The instance factory method instantiation is similar to the static factory method instantiation. The instance factory method used for instantiation is located in another existing bean, the container will call the Bean Factory method to create a new bean instance to use this mechanism, classProperty must be empty, and factory-beanThe property must be specified as the name of the bean containing the factory method in the current (or its ancestor) container, and the factory method of the factory Bean must pass factory-methodAttribute (see the following example ).
<!-- the factory bean, which contains a method called createInstance() --><bean id="myFactoryBean" class="...">  ...</bean>  <!-- the bean to be created via the factory bean --><bean id="exampleBean"      factory-bean="myFactoryBean"      factory-method="createInstance"/>
Although the mechanism for setting bean properties is still mentioned here, it is implicitly managed by the factory bean and configured by dependency injection (DI. In essence, BeanFactoryIt is only an advanced factory interface that maintains bean definitions and dependencies. Pass BeanFactoryWe can access bean definitions. The following example creates a bean factory that reads bean definitions from the XML file:
InputStream is = new FileInputStream("beans.xml");BeanFactory factory = new XmlBeanFactory(is);
This is basically the case, and then use getBean(String)Method to obtain the bean instance; BeanFactoryThe provided method is extremely simple. It only provides six methods for the customer's code to call:
  • boolean containsBean(String): IfBeanFactoryReturns true if the bean definition (or bean instance) contains the given name.
  • Object getBean(String): Return the bean instance registered with a given name. According to bean configuration, if Singleton mode is used, a shared instance is returned. Otherwise, a new instance is returned. If the specified bean is not found, this method may throwBeansExceptionException (will actually throwNoSuchBeanDefinitionExceptionException). Exceptions may also be thrown during bean instantiation and preprocessing.
  • Object getBean(String, Class): Return the bean instance registered with a given name and convert it to an instance of the given class type. If the conversion fails, the corresponding exception (BeanNotOfRequiredTypeException) Will be thrown. The abovegetBean(String)The method also applies to this rule.
  • Class getType(String name): ReturnsClass. If the specified bean instance is not foundNoSuchBeanDefinitionExceptionException.
  • boolean isSingleton(String): Determines whether the bean definition (or bean instance) of the given name is in singleton mode (Singleton will be discussed in bean scope). If the bean is not found, it will be thrownNoSuchBeanDefinitionExceptionException.
  • String[] getAliases(String): Returns all aliases of the given bean name.
========================================================== ==================================== Beanfactory its responsibilities include: instantiate, locate, configure objects in the application, and establish dependencies between these objects. Factorybean ), the function is to generate other bean instances. Generally, this bean has no special requirements. You only need to provide a factory method to return other bean instances. Other bean instances generated by factory beans are no longer generated by spring containers. Therefore, unlike common bean configurations, class elements are no longer required. ========================================================== ========================================================== ========================================================== ====== Proxyfactorybean is used to create a proxy (based on the bean generated by advisor, that is, the targetbean proxy) Our advisor, pointcut, and so on, the ultimate goal is to create this proxy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.