Fundamental principles of IOC and AOP

Source: Internet
Author: User

The IoC (inversion of control) is the relationship between the container control program, rather than the traditional implementation, directly manipulated by the program code. This is where the concept of "inversion of control" is called. Control is transferred from the application code to the external container, and the transfer of control is called inversion. The IOC has another name-"Dependency injection (Dependency injection)". It is understood from the name that the so-called dependency injection, that is, the dependencies between components is determined by the container at run time, and figuratively speaking, that is, the container dynamically injects some kind of dependency into the component.

IOC is an idea that has arisen in recent years, not just programming ideas. The main is to coordinate the dependencies between the components, while greatly improving the portability of components, and the reuse of components more opportunities. In the traditional implementation, the program internal code to the relationship between the control program. We often use the New keyword to implement a combination of two sets of key relationships, which creates a coupling between components (a good design that not only implements code reuse, but also decouples the relationships between components). The IOC solves this problem very well, and it manages the relationship between components from the inside of the program that refers to the external container. This means that the container will dynamically inject a dependency between components in the component at run time. The implementation of the relationship between the control program is given to the external container to complete.

There are two types of IOC implementations:

Dependency Lookup: The container provides callback interfaces and context to the component. Both EJB and Apache Avalon are used this way.

Dependency Injection: The component does not do a location query, but provides a common Java method for the container to determine the dependency relationship. The container is solely responsible for assembly of the component, which passes the object conforming to the dependency through the JavaBean attribute or the constructor to the desired object.

The practice of injecting dependency through the JavaBean attribute is called SetPoint method injection (Setter injection);

The practice of passing a dependency as a constructor child parameter is called a construction sub-injection (Constructor injection).

AOP is aspect oriented programming abbreviation, meaning: aspect-oriented programming, through the pre-compilation method and run-time dynamic agent implementation in the case of non-modification of the source code to the program dynamically unified add the function of a technology.

AOP is mainly used for logging, performance statistics, security control, transaction processing and so on.

1. AOP Related concepts:

1) aspect (Aspect): A focus of modularity, this focus implementation may be additional crosscutting multiple objects. Transaction management is a good example of crosscutting concerns in the Java EE application. The aspect is implemented with spring's advisor or interceptor.

2) Pointcut (Pointcut): Specifies a collection of connection points to which a notification will be raised.

3) connection point (Joinpoint): an explicit point during program execution, such as a call to a method or a specific exception being thrown.

4) Notification (Advice): The action performed by the AOP framework at a specific connection point.

5) Target object: The object that contains the connection point, also known as the notified or Proxied object.

6) AOP agent (AOP proxy): An AOP framework-created object that contains notifications. In spring, an AOP proxy can be either a JDK dynamic agent or a cglib proxy.

7) Introduction (Introduction): Add a method or field to the class being notified. Spring allows the introduction of new interfaces to any notified object.

8) Weaving (Weaving): assembly aspect to create a notified object.

2, the Java domain AOP implementation has many kinds of schemes, mainly includes:

1) AspectJ (TM): Created in Xerox PARC. Has nearly 10 years of history, technology mature. However, it is too complex to destroy encapsulation, and requires a dedicated Java compiler for poor usability.

2) Dynamic Agent AOP: implemented using the dynamic proxy API or bytecode bytecode processing technology provided by the JDK. Specific projects based on the dynamic Agent API are: JBoss 4.0 JBoss 4.0 Server.

3) byte-code-based AOP, such as Aspectwerkz, CGlib, spring, etc.

Fundamental principles of IOC and AOP

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.