Spring's IOC and AOP explained in detail

Source: Internet
Author: User

1. Explain Spring's IOC? Several ways to inject dependency? What are the advantages of spring?

IOC you think of him as a container for the production and management of beans, and the thing that needs to be new in the calling class is now produced by this IOC container, and, if it is a singleton bean, he can also give the life cycle of managing the bean!

Spring's IOC has three ways to inject:

The first is the injection of the set method according to the attribute injection;

The second type is injected according to the construction method;

The third is based on annotations to inject, this way I think it is better, convenient, if the bean is more, the use of the first two methods will make the configuration file too bloated.

Spring's advantages: it's mainly based on its IOC and AOP. I feel like he's just taking the factory pattern and the proxy mode we used in a package. IOC mainly solves the problem of coupling of code, and AOP is the best explanation for slicing programming!

2. Interpretation of Spring in IOC, DI, AOP

The IOC is either control rollover or dependency injection. In layman's parlance, if you need an object somewhere, you don't have to go through new to generate the objects you need, but instead, you will have an object like this through spring's bean factory.

AOP is a tangent-oriented programming. For example, every time you do a database operation, a log is generated. If you have many classes of operations on the database,

So you have to write about the log in every class. But if you're using AOP, then you can write a method that has a way of working with the database,

Each time this method is called, it is added to the operation that generated the log.

ioc/aop/Agent for 3.spring

The IOC is a control reversal and is the core idea of spring. Dynamic reliance on business components is achieved through interface-oriented programming. AOP is aspect-oriented programming, not only in spring or Java, but also in object-oriented programming (OOP), which is a relatively different kind of programming idea.

Spring uses the Java Proxy mechanism to implement AOP programming. Personally think that the Java agent mechanism is really magical. Not a lot of core content

The IOC of the 4.spring is decoupling, what is AOP for?

AOP-oriented aspect programming encapsulates cross-business logic (such as security, logs, transactions, etc.) in a program into a tangent plane, which is then injected into the target object (the specific business logic).

For example: Many methods may throw an exception, you have to record this exception to the log, you can write an interceptor class, in this class to log,

Configuring an AOP Interceptor for these methods of logging in Spring.xml calls this interceptor after the method executes and logs the log.

This way, you do not have to manually log logs every time you throw an exception. Spring's transaction management uses AOP, which can also improve the cohesion of the program.

5. Describe the IOC and AOP in spring in Java

IoC: The straightforward point is to specify the Java class (the full string of the class name) that needs to be instantiated through the configuration file (XML or. properties).

Include a set of initialization values for the Java class, and then we load the configuration file in code, and then we can get an object of that Java class through the. Getbean () function, and the object has been initialized based on the property values specified in the configuration file.

AOP: This is simpler than IOC, and it's straightforward to implement a series of custom statements before or/And after a call to a method.

6. Brief introduction of IOC and AOP in the spring framework

IOC: Inversion of control is a design pattern. One layer of meaning is the transfer of control: from the traditional control of dependencies in the program to the control by the container;

The second layer is dependency injection: separating interdependent objects and describing their dependencies in the spring configuration file. Their dependencies are only established when they are used.

AOP: Facing facets, is a programming idea, the continuation of OOP. The non-core business of the system is extracted and processed separately. such as transactions, logs, and security.

Spring's AOP and IOC are all designed to solve the problem of high system code coupling. Make code reuse high and easy to maintain.

But AOP and IOC are not unique in spring, but spring makes them more flexible and convenient to use.

Spring's IOC and AOP explained in detail

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.