SpringIoc container details

Source: Internet
Author: User

SpringIoc container details
Whether it is j2ee development or j2se development, spring is a big tool. 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! The Ioc container introduces 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. Spring-ioc uses IOC. We do not need to create B objects when creating A object, because B objects are created by IOC and used for use. "How good is it ". What are the benefits of using Ioc to retrieve 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, spring provides functions that can be configured in xml mode. disadvantage: Every class must be registered and configured, Which is cumbersome! 2. annotation configuration method: Use <context: component-scan base-package = "www.xufei.com"/> to configure spring to scan annotations under a specified package during container initialization, automatically registered to the IOC container, use the following: @ Component, @ Service, Controller, @ repository. The functions of these annotations are not different, but they are used to or named differently, corresponding to the service layer, control layer, and DAO layer in development respectively. Advantages: Very simple, strongly recommended disadvantages: annotation is actually intrusive Code, some functions are not powerful xml 3. java Configuration method: Use @ Configuration Annotation on java class, use the @ Bean annotation to create an object to register the object returned by the method to the IOC container. Advantage: the process of creating an object is clear. disadvantage: too many configurations are great! Generally, it is used when the built object is very complex. We need to manually initialize the object value to facilitate modification and maintenance.

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.