Open-Tao Spring3 (2.2)-IoC container fundamentals and Helloword

Source: Internet
Author: User

2.2.1 Concept of the IOC container

An IOC container is a container that has dependency injection capabilities, and the IOC container is responsible for instantiating, locating, configuring objects in the application, and establishing dependencies between those objects. Applications do not need to be directly related to new objects in the code, and the application is assembled by an IOC container. Beanfactory is the actual representative of the IOC container in spring.

How does the Spring IOC container know which objects it manages? This requires a configuration file, and the Spring IOC container instantiates and assembles individual objects in the application by reading the configuration metadata from the configuration file. Configuration metadata is typically used based on an XML configuration file, and spring is fully decoupled from the configuration file, and can be configured in any other way possible, such as annotations, Java file-based configuration, and property file-based configurations.

What is the name of the spring IOC container management object?

The concept of 2.2.2 beans

The objects that make up your application, which are managed by the IOC container, are called beans, which are the objects that are initialized, assembled, and managed by the Spring container , except that the bean is no different from the other objects in the application. How does the IOC determine how to instantiate beans, manage dependencies between beans, and manage beans? This requires the configuration of metadata, which is represented in spring by beandefinition, which is explained in detail, and the configuration metadata specifies how the bean is instantiated, how the bean is assembled, and so on. The concept is almost there, let's do a simple example.

2.2.3 Hello World

Since this complete spring Hello World has been completed, it is not very simple, let's dive into the next container and Bean bar.

2.2.4 Detailed IOC container

The representative of the spring IOC container is the Beanfactory interface in the Org.springframework.beans package, and the Beanfactory interface provides the most basic functions of the IOC container While the ApplicationContext interface under the Org.springframework.context package extends the beanfactory, it also provides integration with spring AOP, internationalization processing, Event propagation and providing different levels of context implementations (such as Webapplicationcontext for Web applications). Simply put, Beanfactory provides the most basic functionality of the IOC container, while ApplicationContext adds more support for enterprise-class functionality. ApplicationContext fully inherits the Beanfactory, so the semantics of beanfactory also apply to ApplicationContext.

Container implementations at a glance:

xmlbeanfactory:Beanfactory realizes, provides basic IOC container function, can obtain resources from classpath or file system etc.

(1) file File = new file ("Filesystemconfig.xml");

Resource Resource = new Filesystemresource (file);

Beanfactory beanfactory = new Xmlbeanfactory (Resource);

(2)

Resource Resource = new Classpathresource ("Classpath.xml");

Beanfactory beanfactory = new Xmlbeanfactory (Resource);

classpathxmlapplicationcontext:applicationcontext Implementation, obtaining configuration files from Classpath;

Beanfactory beanfactory = new Classpathxmlapplicationcontext ("Classpath.xml");

filesystemxmlapplicationcontext:applicationcontext Implementation, obtaining configuration files from the file system.

Beanfactory beanfactory = new Filesystemxmlapplicationcontext ("Filesystemconfig.xml");

Please refer to Cn.javass.spring.chapter2.InstantiatingContainerTest.java for specific code.

Introduction to the ApplicationContext interface Get Bean method:

Object Getbean (String name) returns a bean by name, and the client needs to type-convert itself;

• t Getbean (String name, class<t> requiredtype) returns a bean based on the name and the specified type, and the client does not have to make the type conversion itself, and if the type conversion fails, the container throws an exception;

• t Getbean (class<t> requiredtype) returns a bean based on the specified type, and the client does not have to make the type conversion itself, and if there are no or more than one bean, the container throws an exception;

map<string, t> getbeansoftype (class<t> type) returns a key value for the name and value of the Bean object based on the specified type map, and returns an empty map if no bean object exists.

Let's look at how the IOC container works. Here we analyze it in the form of XML configuration:

Prepare the configuration file : Just like in front of the Hello World configuration file, declaring the bean definition in the configuration file is the metadata for the bean configuration.

second, by IoC the container parses the meta data: The bean Reader for the IOC container reads and parses the configuration file, generates beandefinition configuration metadata objects by definition, and the IOC container instantiates, configures, and assembles the beans based on the beandefinition.

third, the instantiation of IoC container: The container is instantiated by the client to obtain the required bean.

The whole process is not very simple, the implementation of procedure 2-5, in fact, the IOC container is very easy to use, mainly how to do the bean definition. In the next chapter we describe the definition bean in detail.

2.2.5 Summary

In addition to the code of the test program, that is, the program entrance, all the code does not appear any spring components, and all of the code we write does not implement the framework-owned interface, it is very easy to replace the spring, is not a non-intrusive.

Client-side code is fully interface-oriented programming, without knowing the implementation class, you can modify the configuration file to replace the interface implementation, the client code does not require any modification. is not low-coupling.

If there is no real implementation at the beginning of the development, we can simulate an implementation to test, not the coupling code, is not very convenient to test.

There is little dependency between beans, and it is not easy to reuse.

Open-Tao Spring3 (2.2)-IoC container fundamentals and Helloword

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.