Spring two core IOC and AOP

If that says Spring (Please provide details about the spring's two core) on your resume this question will be asked.

First, what is IOC 

1. IOC full name (Inversion of Control) - inversion of control.

IOC is just a design concept, concrete realization of DI (Dependency Injection) - Dependency Injection

● Who controls who and what control: traditional Java SE programming, we directly create objects through new inside the object, a program initiative to create dependent objects; and IoC is a special container to create these objects, which come from the Ioc container create a control object; who controls? Of course IoC container control object; what control? That is the main control external access to resources (including not just objects such as files, etc.). 
● Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in an object to acquire control directly dependent objects, that is, forward; while the reverse is from the container to help create and inject dependent objects; why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is the reverse; what reversed? The acquisition is dependent on the object reversed. 

2. IOC can do

It can guide us how to design a loosely coupled, better program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible. 

Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible. 

3. DI stands for (Dependency Injection) - dependency injection

Dependencies between components during operation is determined by the container, i.e. a container dynamic dependencies injected into the assembly. DI have to understand two ways to understand:

● "who rely on who, why should rely": application depends on the IOC container, because of the need to provide IOC container external resources required object. 

● "Who injection, into what": IOC container will inject an object of the application, into an object of external resources required.


Popular terms is when a role requires the assistance of another role when, in the traditional program design process, usually to create an instance of the caller by the caller. But in the spring

Creating the caller's work is no longer done by the caller, so called inversion of control. Create a callee work done by the spring, and then injected into the caller.

Second, what is AOP

Full 1. AOP (Aspect Oriented Programming) - Oriented Programming
  Aspect Oriented Programming (AOP) is a supplement to object-oriented programming (OOP) is

2. AOP using called "cross" technique, the inside cross-sectional unlock the object of the package, and a plurality of classes that affect the behavior of encapsulated into a common reusable modules, and entitled "Aspect", That aspect. The so-called "aspect", simply put, is unrelated to those of the business, but is a logical or liability service module jointly called encapsulated, such as logging, easy to reuse the code reduction system, to reduce the coupling between modules, and have conducive to the future operability and maintainability.

The core principle: the use of dynamic proxy design pattern before and after the implementation of the method or abnormalities associated logic often do join 

We use AOP to do: 
1) transaction: a transaction before the execution method to open and close the transaction after execution is completed, the transaction is rolled back after an exception occurs 
2) permission judgment: Before the implementation of the method, has the authority to determine whether 
3) Log: Before performing for log processing. 

2. Spring AOP principle: JDK dynamic proxy

 Dynamic proxy implementation principle: a class that implements the interface can do dynamic proxy. The steps we need to realize that:

First, write a way to achieve a InvocationHandler interface method calls will be forwarded to invoke the class () method.

Then when you need to use Hello, access to the proxy object Hello JDK dynamic proxies.

These are the dynamic proxy achieve the JDK, we realized invoke methods in interface invocationhandler our logical operations may be implemented, require interface reference class, with reference to the interface with a constructor parameter as parameters, to rewrite the invoke method

We need to write the code in the agent's time

Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler handler)

Parameter 1, i.e. implement the interface proxy object class loader

2 parameters, the interface implemented

3 parameters, the interface classes implement invocationhandler

Therefore, JDK agency will forward all the way to achieve the interface to invoke the method invocationhandler, we can achieve here any logical requirements

 

Guess you like

Origin www.cnblogs.com/klyjb/p/11402627.html