(2) DI / IOC and AOP learning principles acquaintance --Spring Review

Author: Chen Buyi
www.cnblogs.com/chenbenbuyi

Spring series of articles
(1) a thorough understanding of the Spring container and application contexts --Spring review learning
(2) DI / IOC and AOP principle acquaintance --Spring review learning
(3) automated assembly bean - Spring retrospective study
(4) through Java annotations and XML configuration assembly bean - Spring retrospective study
(5) AOP programming --Spring review section to learn learning --Spring review

Foreword

As farmers engaged in java code, Spring importance of self-evident, you may have to deal with day in and Spring framework. Spring aptly named, to develop java application with spring-like feeling refreshed. Spring, can be said to be an essential basis for any java developer a leading high-end technology. Of course, to learn Spring, especially the Spring understand the underlying principle is not easy, it takes a lot of time and energy to devote themselves to the study, and in the actual projects ongoing and summary trial and error in order to understand the formation of their own thinking. Bloggers initially quite shallow understanding of Spring, the project encountered problems depend on the degree of your mother probably can cage and integration of the solution. But then, so contacted more than a year in Spring, cognitive framework of its system more messy, deep technology is still smoke and mirrors general, did not form their own knowledge and understanding, which enhance the programming technique is very unfavorable . In view of this, decided to learn the Spring Framework stop the system from start to finish and record the learning bit by bit in the form of blog to share technical knowledge, be initiate it. Well, without further ado, Syria, let's cut to the chase began -

Spring Framework Core Introduction

DI (Dependency Injection), dependency injection , and we often hear of another concept IOC (Inversion of Control) In fact, in the final analysis is the function of the same, just the same functions different point of view to explain nothing. Here bloggers do not go too much discrimination, there are a lot of interpretation on the degree of your mother. We need to know is, what is called dependency injection, why dependency injection. Find out these two points, I want to learn to Spring's ideologically even on the road.

When using Spring useless - that is not dependent on the injection time, between classes java application functionality to achieve mutual cooperation is more strenuous, a class (A) to fulfill its functions if needed dependent on another class (B) collaboration, then you need to take the initiative to create an object of class B in the class a, class B in order to use the completion function (here Tell me what the situation is not going to tangle like a static method). This is tantamount to a Class A Class B will be responsible for managing the entire life cycle of an object. In extremely simple case, in a new class of objects out of another class does not seem a problem, but the complexity of the application class collaboration with the class tend to be multilateral, we do not know the function will implement a class how many alternative object dependencies to collaborate, so create their own objects in the class and object management throughout the life cycle, it will cause unimaginable complexity and highly coupled code. Well, imagine if we can target the life cycle of third-party components to be managed, when a class requires additional third-party components to create objects out to direct it, so that classes can only focus on their features implementation, without having to manage the life cycle of other types of objects, such kind of feature is simply a lot. Yes, you must have understood, Spring (container) is the third-party components. We just need to tell Spring (container) which objects need to manage on the line, you do not have to care about how the Spring Framework to create an object. Thus, when a class A Type B objects, if class B has been declared to the container Sping administration, when the program is run to the class A Type B, on the Spring container by injection-dependent manner, the object class B injected into class A and assist business functions. By relying on third-party components injection, objects no longer need to create their own and manage dependencies between classes and the class. Creating Dependency injection manner has a variety of objects, such as injection interfaces, constructors implantation, injection, etc. The method of the setter. Here, you should have a relatively straightforward recognition of dependency injection. As to why dependency injection, above, has made it very clear, is to reduce the degree of coupling between the code components, we first intuitive feel for dependency injection than he managed objects advantage of it by a simple example -

public class Man implements Human {
   private QQCar car;
   public Man() {
       this.car = new QQCar();
   }
   @Override
   public void xiabibi() {
   }
   public void driveCar(){
       car.drive();
   }
}

There are two implementations of the interface temporarily Car: Mercedes-Benz and QQ car, the code above Man classes and class QQCar highly coupled, the old driver by constructor creates only the QQ car object, so only open QQ car, the old driver tried Mercedes how to do it, you let him re-create the Mercedes object? Such highly coupled code seems to be no way, then, we do object by way of injecting some improvements to the code above:

public class Man implements Human {
   private Car car;
   public Man(Car car) {
       this.car = car;
   }
   @Override
   public void xiabibi() {
   }

   public void driveCar() {
       car.drive();
   }
}

The code above multi-state characteristics, shielded by the constructor interface injection way off the concrete realization of the object, so that older drivers will be able to think what car to open the car up. This is the benefits of dependency injection.

AOP (Aspect Oriented Programming), aspect-oriented programming . Daily development, we have completed a business function, when writing a bunch of code to the end code optimization, we found that the real business of the completion code might then two, and the rest are not part of the business-related degrees big, just to achieve some kind of code technology, is completely pulled out, so naturally, we will extract into a utility class, who used this place look simply call the utility method to ok . Let us stand a little higher to see, functional components of various business modules in addition to completing the related business functions, are involved in extra operating logs, transactions, security, control, and so on. These are not the core function module, but indispensable. If these extra features added to the code, each component business systems and it looks set to repeat too much, and let business code seems confusing, not pure. This time, you ask God, can you let your business code only focus on achieving business and what logs, transaction and other irrelevant things never mind? Oh, God said no problem, so there is AOP. If the goal is to rely on the injection components that work together to maintain a relatively loose coupling state, then, AOP sucked across the entire application function separate reusable components is formed . Popular point that is reusable component log, affairs, we can be dispersed in the logs, transactions, security and other functions throughout the business code codes pulled out into a single tool components, will be in the Spring configuration which was declared as a feature section, then what you want to tell Spring where, what time to use (cut) these reusable components on the line. This is my simple interpretation of aspect-oriented. This article is just a primer, so bloggers will simply elaborate on the concepts, not specific code, configurations, will continue in the offer in a subsequent blog post, please Paizhuan.

Guess you like

Origin blog.csdn.net/qq_40914991/article/details/91367462