Spring's IOC container first series

Source: Internet
Author: User

One. S Overview of the IOC container for Pring

The process of spring's IOC is also called Dependency Injection (DI), so objects can define their dependencies through constructor arguments, parameters of factory methods, or properties set on the object instance that the factory method constructs or returns, and the container injects these dependencies when the bean is created. Spring implements the IOC container based on org.springframework.be and Org.springframework.context.

For spring's common design patterns and applications, please click here for "Spring common design patterns and Applications"

The core interface Beanfactory interface provides an advanced configuration mechanism to manage any type of object. ApplicationContext is a sub-interface beanfactory. It adds more ease of integration with spring's AOP features; Beanfactory provides a configuration framework and basic functionality, and ApplicationContext adds more enterprise-specific functionality.
In spring, objects that form the backbone of the application and are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by the spring IOC container.

View of how Spring works:

two. S The way of the Pring IOC

Spring Container XML Configuration Management
It is usually composed of multiple bean definitions. XML-based configuration metadata configures these beans as <beans/> elements within <bean/> top-level elements
Provide spring's official profile template (footage source Www.spring.io)

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd ">

<bean id= "..." class= "..." >
<!--collaborators and configuration for this bean go
</bean>

<bean id= "..." class= "..." >
<!--collaborators and configuration for this bean go
</bean>

<!--more beans definitions Go

</beans>

1. default constructor

When you create a bean by way of a constructor, all ordinary classes can be used and compatible by spring. In other words, the class being developed does not need to implement any particular interface or encode in a specific way. Simply specifying the bean class is sufficient. However, depending on the IOC type you use for a particular bean, a default (empty) constructor is required.

Using XML-based configuration metadata, you can specify your bean class as follows:

<bean id= "Examplebean" class= "examples. Examplebean "/>

<bean name= "Anotherexample" class= "examples. Examplebeantwo "/>


Where the class in the bean is the full path of the bean we have to initialize in spring.

2. Static Factory

When you define a bean created using a static factory method, you can use the Class property to specify a property that contains the static factory method's class and Factory-method specifies the name of the factory method itself. You should be able to call this method (using the optional arguments described later) and return an instantiated object, which is then considered to be created by a constructor. What is used for this bean definition is created as a static factory method.
Definition of a static factory

Public class ClientService {

Private Static ClientService ClientService = new clientservice ();

Private ClientService () {}

Public Static ClientService CreateInstance () {
return ClientService;
}
}


Configuration XML

<bean id= "ClientService"
class= "examples. ClientService "
factory-method= "CreateInstance"/>

The use of such a spring framework in invoking an IOC instantiation is to create an object by using the static method in the call to the statically factory by reflection creation. At this point the object being created is in the hands of the static method.

3. Example Chemical Factory

An instance of a chemical factory is the same as a static factory method, which simply reflects a non-static method of an existing object in an instance factory, thus going to the spring container to create the instantiated bean.
Create an instance chemical plant

public   class  defaultservicelocator {
 
     private   Static  clientservice ClientService = New  clientserviceimpl ();
 
     private   Static   Accountservice Accountservice = new  accountserviceimpl ();
 
     public  clientservice createclientserviceinstance () {
         return  clientService;
    }
 
     public  accountservice createaccountserviceinstance ( {
         return  accountService;
    }
}


Configuration xml:

<bean id= "Servicelocator" class= "examples. Defaultservicelocator ">
</bean>

<bean id= "ClientService"
Factory-bean= "Servicelocator"
factory-method= "Createclientserviceinstance"/>

<bean id= "Accountservice"
Factory-bean= "Servicelocator"
factory-method= "Createaccountserviceinstance"/>

three . Knot Bundle

The three ways mentioned above are the three types of initialization beans that our spring provides. Have you learned it? Next time we come together to study di in four Ways

Spring's IOC container first series

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.