Spring Ioc Learning (1)

Source: Internet
Author: User

Content included: What is Ioc, role, bean creation, method, configuration, set, scope, different roles and bean Dependencies

Ioc Control Inversion (Inversion of Control) is also called DI Dependency Injection ). in programming, the form of combination is often used, that is, other class objects are used in a class. Ioc is used when this class is instantiated, the related objects used in it will also be instantiated. Instantiation methods include ① constructor parameter ② method parameter ③ attribute.

Spring container is started to read the configuration file through BeanFactory or ApplicationContext. The basic configuration of this configuration file is as follows:


Container instantiation method:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"service.xml","daos.xml"});

The service. xml Code is as follows:


The daos. xml Code is as follows:



The configuration file shows that PetStroreServiceImpl has two member variables: SqlMapAccountDao and SqlMapItemDao.

In this way, Spring Ioc will instantiate two other instance variables when creating PetStroreServiceImpl.

You can use the import attribute to separate configuration files for ease of management.


Pay attention to the file location, service. the relative path used by xml must have service in the current directory. xml file. for messageSource. xml and themeSource. xml must be under resources. The preceding Slash can be taken without it, but it is generally not recommended.

Container usage:


In the configuration file Is a BeanDefinition object. The bean definition has the following attributes:



Bean instantiation:

1. The most basic configuration:


2. Static workshop method:

3. entity workshop method: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140508/20140508083228133.jpg" alt = "\">

Dependency injection:

We can see from the above: dependency injection can be performed in three ways: 1. constructor parameters; 2. Factory method parameters; 3. Attribute injection.

Use method injection. In most application scenarios, the bean we use is a Singleton, but there is a situation where a singleton class contains a non-singleton member variable.


Bean scope:


When the scope of a Bean is defined as Singleton, The spirng Ioc container creates an instance and puts it in the cache. For all requests, the Bean in the cache will be returned. Spring uses a singleton bean by default.


Prototype scope:



The Spirng container does not manage the entire lifecycle of prototype beans.


In a singleton bean, you have prototype-bean:


The three scopes of Request, Session, and Global Session are only available after they are combined with the web. Initialize web Configuration:


Definitions of bean dependencies between different web scopes:



The following situation is different from the above:



The org. springframework. beans. factory. config. Scope interface must be implemented for Custom scopes.

In this case, I feel that I am using less resources.



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.