Beanfactory and ApplicationContext Introduction

Source: Internet
Author: User
Tags event listener

Class structure of Beanfactory

Listablebeanfactory: This interface defines several ways to access bean basic information in a container, such as the number of beans to be viewed, the number of a type of bean to get, the configuration name of a certain type of bean, the view of whether the container contains a certain kind of bean, etc.

Hierarchicalbeanfactory: Parent-child Cascade IOC container interface, sub-container access to parents container through interface method

Configurablebeanfactory: This is an important interface that enhances the customization of IOC containers. It defines the set-class reprint, the property editor, the container initialization, the post-processor method, etc.

Autowirecapablebeanfactory: Defines the automatic assembly of beans in a container as a rule, such as name matching or type matching

Singletonbeanregistry: Defines a method for registering a single instance bean with a container at run time

Each <bean> node element in the beandefinitionregistry spring configuration file is represented by a Beandefinition object in the Spring container, which describes the bean's configuration information and The Beandefinitionregistry interface provides a way to manually register Beandefinition objects with the container

Case initialization Beanfactory provides a configuration for car through the spring configuration file, and then launches the SPRINGIOC container via the beanfactory mount configuration file

Beans.xml File Car configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd ">   <BeanID= "Car1"class= "Com.smart.Car"Init-method= "Myinit"Destroy-method= "Mydestory"P:brand= "Red Flag CA72"P:maxspeed= "$"/></Beans>

Test code

    Public Static voidGetbeantext ()throwsIOException {//get the default implementation of the resource loaderResourcepatternresolver res =NewPathmatchingresourcepatternresolver (); Resource Resource= Res.getresource ("Classpath:com/example/beanfactory/beans.xml"); System.out.println (Resource.geturl ());//to build the default implementation class for BeanfactoryDefaultlistablebeanfactory factory =Newdefaultlistablebeanfactory (); Xmlbeandefinitionreader Reader=NewXmlbeandefinitionreader (Factory);        Reader.loadbeandefinitions (Resource); System.out.println ("Init beanfactory"); //The Bean object is initialized the first time it is calledCar car = Factory.getbean ("Car", car.class); System.out.println ("Car bean is ready for use!");    Car.introduce (); }
View Code

Xmlbeandefinitionreader Loads the spring configuration information via resource and launches the IOC container, which can then be obtained from the IOC container through the Beanfactory#getbean (Beanname) method. When the beanfactory starts the IOC configuration, it does not initialize the bean in the configuration file, and the initialization action occurs at the first call. For a single Instance bean (singleton), beanfactory caches the bean, and the bean object is fetched directly from the cache of the IOC container when the bean is fetched in the second Getbean ()

Spring provides a buffer for the instance bean of the Defaultsingletonbeanregistry class, which is a hashmap implemented cache, and a single-instance bean is saved in this hashmap with the Beanname key

ApplicationContext Introduction

ApplicationContext's main implementation class Classpathxmlapplicationcontext and Filesystemxmlapplicationcontext, the former default load configuration file from the class path, The latter loads the configuration file from the file system by default

The interface methods that you have:

Applicationeventpublisher: Lets the container have the ability to publish the application context, including container start events and shutdown events. Beans that implement the Applicationlistener event listener interface can accept container events and respond to events. There is a applicationeventmulticaster in the ApplicationContext abstract implementation class Abstractapplicationcontext, which is responsible for saving all listeners, Notifies these event listeners when a container generates a context event.

Messagesource provides I18 international access for Applications

Resourcepatternresolver all ApplicationContext Implementation classes implement a function similar to Pathmatchingresourcepatternresolver, Spring configuration files can be loaded via a prefix ant-style resource file path

LifeCycle: This interface provides the start () and stop () methods

Configurableapplicationcontext extension ApplicationContext, it adds two methods: Refresh () and close (), so that ApplicationContext has a start, The ability to refresh and close the application context. Call Refresh () when the application context is closed to start the application context, and call Refresh () in the state that is already started to clear the cache and reload the configuration information.

  // instantiate the Bean object     when initializing the context Classpathxmlapplicationcontext loading files By default in the Classpath   can also set the ant prefix of the loaded file        new Classpathxmlapplicationcontext ("Com/example/beanfactory/beans.xml");
When initializing the context, instantiate the Bean object Filesystemxmlapplicationcontext default in the file system path download configuration, you can also set the ant prefix of the loading file
Application Context=new filesystemxmlapplicationcontext ("Com/example/beanfactory/beans.xml");

Beanfactory and ApplicationContext are different when initializing an object, Beanfactory does not instantiate the bean in the configuration file while initializing the context, but only instantiates it the first time it is invoked. ApplicationContext to instantiate a Bean object when the context is initialized

Configuration information provided in annotated Java classes

// represents a configuration information provider class @Configuration  Public class Beans {    //     define a Bean class    @Bean (name = "Car")    public  Car Buildcar () {        new  car ();        Car.setcolor ("Blue");        Car.setbrand ("Red Flag");        Car.setmaxspeed ($);         return car;}    }

Spring provides a dedicated ApplicationContext implementation annotationconfigapplicationcontext for annotation class-based configurations, using this class to start the Spring container

// initializing the annotation-based Bean class     Public Static void textannotation () {        new annotationconfigapplicationcontext (Beans.  Class);         = Context.getbean ("Car", car.  Class);        Car.introduce ();    }

Spring also configures classes for groovy, providing a dedicated ApplicationContext implementation Genericgroovyapplicationcontext that uses this class to start the Spring container

Beanfactory and ApplicationContext Introduction

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.