Spring-IOC containers and spring-ioc containers

Source: Internet
Author: User

Spring-IOC containers and spring-ioc containers
Chat

Spring is a huge tool for both j2ee development and j2se development. Spring is the most irreplaceable among the three popular ssh frameworks. If hibernate and struts are not used, I don't think it is important, but it cannot be used without spring, some people may say that spring is useful? Why is there a new object directly? It's so troublesome and hard to understand when I create an ioc. With so many configurations, I don't feel the value of the new object when I write code, I have always thought that it is to keep learning spring and learn its value with these questions. In fact, spring does not bring about persistence technology, mvc Framework, cache components, and so on. It is a design concept (IOC, AOP) that can be used to accommodate baichuan. The real advanced martial arts are not his tricks!

 

Ioc container Introduction

Ioc-Inversion of Control, that is, "Control Inversion ". No one knows the name of the speaker. If I start, it must be called "object container". Although it is not completely correct, it basically means this. Define the classes we have written and register them to the spring container. We need to use an object instead of a new one, but just get one from the object container. Can a new object be used for object creation? This is the case. Class A references class B. Generally, new B () is directly referenced in Class B. When Class A is used, new A () is initialized, and new B () is used (), if spring is used, get ("A") is used directly, and B is automatically injected into object. This is "control inversion": the right to create an object is reversed to the IOC container. I don't know if I have understood this. Just draw a picture to express it.

 

Using IOC, we do not need to create B objects when creating A object, because the B object is created by IOC and can be used. "How good is it ".

Functions of Ioc

So what are the benefits of retrieving objects from spring containers? I am not as convenient and direct as new objects. Without spring programs, it is still not running as usual. In fact, there is nothing wrong with this. How convenient the new object is, But I believe everyone has heard of "decoupling", that is, to minimize coupling between components in the software, decoupling makes the software easy to maintain and highly reusable. The above class A is not associated with the implementation of Class B at the code level. What if Class B is changed to Class C?

Is there another class B reference in Class?

Yes, there is A reference pointing to the B object, but spring advocates an excuse for programming. Class A holds A Class B interface, therefore, Class C only needs to be the same interface as Class B.

It is precisely with the IOC container's "control reversal" design philosophy that makes spring almost compatible with any popular plug-ins or frameworks.

Does spring-ioc have no drawbacks?

When spring was used for xml configuration, there were too many spring configuration files, which was too cumbersome. However, this problem did not exist after the annotation method was used.

What if your system is too small to use ioc? But it's no harm if you use it. Maybe one day your system is too big?

Three configuration methods of Spring bean

1. xml configuration method:

Advantage: Classic, all functions provided by spring can be configured and implemented in xml format;

Disadvantage: You must register configurations for each class, which is cumbersome!

2. annotation configuration method:

Use<Context: component-scan base-Package="Www.xufei.com"/>Configuration, spring can scan the annotation under the specified package when initializing the container, automatically register it in the IOC container, and use the following :@Component,@ Service,Controller,@ Repository,The functions of these annotations are not different, but there are differences in habits or naming. They correspond to the service layer, control layer, and DAO layer in development respectively.

Advantages: very simple and highly recommended

Disadvantage: Annotations actually intrude into the code, and some functions are not as powerful as xml.

3. java configuration method:

Use the @ Configuration annotation in the java class. Use the @ Bean annotation in the method for creating the object to register the object returned by the method to the IOC container.

Advantage: the object creation process is clearly visible.

Disadvantage: too many configurations are recommended!

Generally, it is used when the built object is very complex. We need to manually initialize the object value to facilitate modification and maintenance.

 

Last

Spring's design philosophy is profound and profound, so we need to continue to learn and think about it. It takes time to understand something. When the time is up, it will be a sudden sensation.

Related Article

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.