Spring is an open source framework that mainly implements two things, IOC (control inversion) and AOP (sectional-oriented programming).
IOC
Control inversion, also known as dependency inversion.
The so-called dependency, from the perspective of the program, means that for example, if A wants to call B's method, then A depends on B. Anyway, if A wants to use B, A depends on B. The so-called inversion, you must understand what will happen if it is not inverted, because A must have B before it can call B. If it is not inverted, it means that A actively obtains an instance of B: Bb=newB(), which is the easiest way to obtain B instances (of course, there are various design patterns that can help you obtain B instances, such as factories, Locator, etc.), and then you can call the b object. Therefore, not being inverted means that A must actively obtain B before using B; at this point, you should understand the meaning of inverted. Inverting means that if A wants to call B, A does not need to actively obtain B, but others will automatically deliver B to the door.
The so-called control reversal is the transfer of control power. To give an example: if a person wants to drive, under normal circumstances, the person should look for the car by himself. After the control reversal is achieved, the person does not need to consider where the car comes from, just drive directly, and the person transfers the control of the car to other objects. Experience the following code
Define an interface Car
public interface Car { void go();}Define two types of cars
public class Benz implements Car { public void go() { System.out.println("benz go..."); }}public class BMW implements Car{ public void go() { System.out.println("bmw go..."); }}Below is a driving
public class Person { Car car=new Benz(); void DriveCar(){ System.out.println("begin drive"); car.go(); }}This is a normal code control process. If you want to drive, you have to instantiate a car by yourself. However, in this case, this person can only drive one car. How can this person be able to drive various cars? It is to achieve control reversal. In other words, if the person no longer instantiates the car himself, how can the person get the object of the car? We can use Dependency Injection (DI for short) to get the object of the car, thereby achieving control inversion. So, we need to modify the Person class
public class Person { Car car=null; public Person(Car car){ this.car=car; } void driveCar(){ System.out.println("begin drive"); car.go(); }}The current Person class no longer instantiates the car's object itself, but uses the constructor to obtain the car's object. Therefore, this class can drive various cars, as long as the car implements the Car interface. See how to use Person class
public static void main(String[] args) { Person p=new Person(new Benz()); p.driveCar();}The Person class can drive more than one type of car as long as you pass it in through the constructor. In this example, the Car object is the dependency of the Person class. When we instantiate the Person class, passing an instance of Car to the Person class is dependency injection. Our Person class thus implements control inversion.
What exactly does the control reversal reverse? There is a saying that: the so-called control inversion is the process of obtaining object dependencies. After the control rights are reversed, the process of obtaining dependent objects changes from its own management to injection by the IOC container.
Spring implements dependency injection
In the above line of code, Person p=new Person(new Benz());, we manually new a Benz() object and inject it into the Person class. Spring doesn't do this because Spring feels that your line of code instantiates a specific Benz class. If you want to instantiate a BMW class here in the future, wouldn't you need to modify the code? Then I'll just write it in the configuration file. Even if you need to pay attention in the future, at least you don't need to modify the code, so the following configuration is available.
<beans> <bean id="car" /> <bean id="person" > <property name="car" ref="car" /> <bean/></beans>
Then, Spring provides some mechanisms. When obtaining the Person class object from the configuration file, the car object it comes in will be assembled, and the person object does not need to worry about which specific class is passed in. Therefore, Spring, as an IOC framework, mainly takes two steps: creating an object and assembling the relationship between objects.
AOP
AOP (Aspect Oriented Programming) is aspect-oriented programming. Let me give you an example of what aspect is. In a complete website project, many modules need to be logged, many places need to be logged in, and many places need to be exception handling. Logging, logging, exception handling and other logics are the so-called sections. Assuming that I write the logic of these sections everywhere, then the maintainability of the code can be imagined. AOP is to achieve separation of concerns, extract the logic of these sections and write them into separate classes, and then find a way to assemble them with general modules to execute. Ordinary modules do not even know that they have assembled them with sections and sections.
The goal of aspect-oriented programming is to separate the focus. What is a focus? It's what you have to do, it's focus. If you are a young man and have no life goals, you just need to wear clothes and eat, and you only know one thing to play all day! So, every day when you open your eyes, you just want to go play after dinner (things you have to do), but before playing, you also need to wear clothes, wear shoes, fold quilts, cook, etc. These things are your focus, but you just want to eat and play, so what should you do? All these things are left to others to do. Before you walk to the dining table, there is a special servant A to help you dress, servant B to help you put on shoes, servant C to help you fold the quilt, servant C to help you cook, and then you start eating and playing (this is your day's business). After you finish your day, come back, and a series of servants start to help you do this and that, and then the day is over!
The advantage of AOP is that you only need to do your important business and others will help you with other things. Maybe one day, if you want to run naked and don’t want to wear clothes, then you just fire your servant A! Maybe one day, you want to bring some money before going out, so you can hire another servant D to help you get money! This is AOP. Everyone performs their own duties and combines flexibly to achieve a configurable, pluggable program structure.
From Spring's perspective, the biggest purpose of AOP is to provide transaction management capabilities. Transaction management is a focus. Your business is to access the database, and you don’t want to manage transactions (too annoying). Therefore, Spring will automatically start the transaction before you access the database, and will automatically commit/roll back the transactions for you after accessing the database!
Look at the following code, it doesn't matter if you don't understand it
<bean id="audience" /><aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression= "execution(* com.springinaction.springidol.Performer.perform(..))" /> <aop:before pointcut-ref="performance" method="takeSeats" /> <!--<co id="co_refPointcut"/>--> <aop:before pointcut-ref="performance" method="turnOffCellPhones" /> <!--<co id="co_refPointcut"/>--> <aop:after-returning pointcut-ref="performance" method="applaud" /> <!--<co id="co_refPointcut"/>--> <aop:after-throwing pointcut-ref="performance" method="demandRefund" /> <!--<co id="co_refPointcut"/>--> </aop:aspect></aop:config>
The roughly meaning of the above configuration is that when the Performer.perform method is about to occur, the proxy in the Spring framework will intercept the target method (Performer.perform()). Before executing the target method, first execute the Audience.takeSeats() and Audienceturn.OffCellPhones() methods, and then run the target method. When the target method is executed and returns, run the Audienceturn.applaud() method. If the target method unfortunately throws an exception, the agent will run the Audienceturn.demandRefund() method. In short, Spring's proxy class monitors the execution of the target method in all aspects, while the target method only focuses on its own affairs and does not even know the existence of the proxy class.
Summarize
The above is all about a simple understanding of Spring's IOC, AOP and code examples. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed introduction to Spring's Ioc simulation implementation
Spring AOP Intercept-Three ways to implement automatic proxy detailed explanation
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!